Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onValuesChange里如果要访问外层 state,拿不到最新的值 #95

Open
xiaobc1234 opened this issue Oct 26, 2022 · 3 comments
Open

Comments

@xiaobc1234
Copy link


const [abnormalTypeOpts, setAbnormalTypeOpts] = useState([])

 const { getFieldDecorator, validateFields, errors, values, setFieldsValue } = useForm({
      onValuesChange: (values) => {
        if (values.orderType !== undefined) {
          setFieldsValue({
             type: abnormalTypeOpts[0] // abnormalTypeOpts拿到的一直是[]
          })
        }
      }
})

useEffect(() => {
 setAbnormalTypeOpts([{name: '测试'}])
});

@xiaobc1234
Copy link
Author

感觉 useForm 需要第二个参数 deps

@mushan0x0
Copy link
Owner

可以把onValuesChange里的逻辑放在useEffect里,然后依赖values

@xiaobc1234
Copy link
Author

这种方式就没办法知道是哪个字段change了吧,我现在用ref缓存了 abnormalTypeOpts 值,倒是可以拿到。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants