form
form copied to clipboard
setFieldsInitialValue doesn't work
online demo: http://react-component.github.io/form/examples/async-init.html
https://github.com/react-component/form/blob/master/examples/async-init.js#L62
- 感觉这个 API 已经失效了。
- 如果只是为了动态修改 initialValue,下面这样就可以了吧?不需要额外的 API
getFieldProps('name', { initialValue: variable })
@yiminghe
看能不能修复, reset 会 设置到 initialValue 的值,以前有些场景需要改 reset 后的值
如果只是为了动态修改 initialValue,下面这样就可以了吧?不需要额外的 API
getFieldProps('name', { initialValue: variable })
这个要 rerender,有个方法简单点
个人认为差别不会很大,首先 setFieldsInitialValue 的调用时机在哪里?如果在组件外部,则和传 props 效果一样,如果在组件内部,则和 setState 相似,都会都可以方便的触发 re-render。
确认了,这个 API 没问题,是例子的写法有问题。
不过观点不变, 这个 API 的意义不大。
除了 demo,这个 setFieldsInitialValue
也有问题,更新后没有触发 re-render https://github.com/react-component/form/blob/master/src/createBaseForm.js#L398-L419
而且这个问题,从一开始就有了 https://github.com/react-component/form/commit/2bcf7b320c9f72f110bdc16fe4961d92369cf15a#diff-d2b74dd398f50983ba983c2fefdf8b6aR291
Hello, i'm confused as to what is going on with this issue. Updated example still seems to be broken. Is there a workaround?
I'm also experiencing some problems with setFieldsInitialvalue
.
What is its recommended usage?
Should it be called before/after all decorators have been declared?
Can it be called at every render or should it be called at componentDidMount
?
Any news ?
After investigation it seems that initialValue
has been updated via setFieldsInitialvalue
in https://github.com/react-component/form/blob/master/src/createFieldsStore.js#L40-L51 but the component is not updated.
The input value become visible only on form submit action. Try to validate others fields will not make it appears.
Use cases :
If you want to set an initialValue
:
<input
{...getFieldProps('email', {
rules: [
{required: true},
{type: 'email'}
],
initialValue: '[email protected]'
})}
/>
If you want to overwrite initialValue using setFieldsInitialvalue
and force component update :
this.props.form.setFieldsInitialValue({
email: '[email protected]',
});
this.forceUpdate()
In my experience I never had to use setFieldsInitialvalue but yes something seems wrong with this.
函数式组件如何 re render:
https://zh-hans.reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate
const [ignored, forceUpdate] = useReducer(x => x + 1, 0)
...
form.setFieldsInitialValue(initialValue) // 该方法不会自己触发 re-render
forceUpdate()
或者使用 react-custom-hook-use-force-update