Как я могу очистить форму с помощью перекомпоновки в реакции? - PullRequest
0 голосов
/ 27 сентября 2018

После отправки формы я хочу очистить ее, но, похоже, это решение не работает.вот мой обработчик отправки:

handleSubmit: ({ title, body }, props) => e => {
    e.preventDefault()
    props
        .handleCreatePost({
            variables: {
                title,
                body
            }
        })
        .then(() => {
            return {
                title: "",
                body: ""
            }
        })
        .catch(err => console.log(err))
}

1 Ответ

0 голосов
/ 27 сентября 2018

Каждый раз, когда вам нужно сменить реквизит внутри вашего компонента, вы должны использовать 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))
      }
    )
)
...