如何在 reactjs 中使用 recompose 清理表单?
How do I clean up form using recompose in reactjs?
表单提交后我想清理它,但这个解决方案似乎不起作用。这是我的提交处理程序:
handleSubmit: ({ title, body }, props) => e => {
e.preventDefault()
props
.handleCreatePost({
variables: {
title,
body
}
})
.then(() => {
return {
title: "",
body: ""
}
})
.catch(err => console.log(err))
}
每次你需要从组件内部更改道具时,你必须使用 withStateHandlers
。
compose(
withStateHandlers(
({title, body})=> ({title, body}), //set the state from parent props
{
setTitle: () => title => ({title}), // update the title
setBody: () => body => ({body}), // update the body
clearProps: () => () => ({titel:'', body: ''}) // create a handler to reset the values
}
),
withHandlers({
handleSubmit: ({ title, body, clearProps }, props) => e => {
e.preventDefault()
props
.handleCreatePost({
variables: {
title,
body
}
})
.then(clearProps) // reset the values
.catch(err => console.log(err))
}
)
)
表单提交后我想清理它,但这个解决方案似乎不起作用。这是我的提交处理程序:
handleSubmit: ({ title, body }, props) => e => {
e.preventDefault()
props
.handleCreatePost({
variables: {
title,
body
}
})
.then(() => {
return {
title: "",
body: ""
}
})
.catch(err => console.log(err))
}
每次你需要从组件内部更改道具时,你必须使用 withStateHandlers
。
compose(
withStateHandlers(
({title, body})=> ({title, body}), //set the state from parent props
{
setTitle: () => title => ({title}), // update the title
setBody: () => body => ({body}), // update the body
clearProps: () => () => ({titel:'', body: ''}) // create a handler to reset the values
}
),
withHandlers({
handleSubmit: ({ title, body, clearProps }, props) => e => {
e.preventDefault()
props
.handleCreatePost({
variables: {
title,
body
}
})
.then(clearProps) // reset the values
.catch(err => console.log(err))
}
)
)