React Native React-Hook-Form 重置

React Native React-Hook-Form Reset

如何 reset/clear 字段,我正在使用 react-hook-form 和 Typescript。

代码:

  const {register, handleSubmit, setValue, errors} = useForm();

  useEffect(() => {
    register('content')
  }, [register]);

  return(

      <S.Input
        onChangeText={text => {setValue('content', text)}}
        multiline
        textAlignVertical="top"
        maxLength={280}
        placeholder="O que está pensando?"
        selectTextOnFocus={true}
      />
      <S.Button disabled={isLoading} onPress={handleSubmit(handlePost)} />

您可以将 Value 设置为默认值,因为该组件不是受控组件或包装在 Controller.

将输入包装在控制器中将允许您使用重置。 https://react-hook-form.com/api/#reset

const {register, handleSubmit, setValue, errors} = useForm();

// to reset the input
setValue({content: '})