form icon indicating copy to clipboard operation
form copied to clipboard

setFieldsInitialValue doesn't work

Open benjycui opened this issue 7 years ago • 12 comments

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

  1. 感觉这个 API 已经失效了。
  2. 如果只是为了动态修改 initialValue,下面这样就可以了吧?不需要额外的 API
getFieldProps('name', { initialValue: variable })

benjycui avatar May 18 '17 01:05 benjycui

@yiminghe

benjycui avatar May 18 '17 01:05 benjycui

看能不能修复, reset 会 设置到 initialValue 的值,以前有些场景需要改 reset 后的值

yiminghe avatar May 18 '17 03:05 yiminghe

如果只是为了动态修改 initialValue,下面这样就可以了吧?不需要额外的 API

getFieldProps('name', { initialValue: variable })

benjycui avatar May 18 '17 03:05 benjycui

这个要 rerender,有个方法简单点

yiminghe avatar May 18 '17 03:05 yiminghe

个人认为差别不会很大,首先 setFieldsInitialValue 的调用时机在哪里?如果在组件外部,则和传 props 效果一样,如果在组件内部,则和 setState 相似,都会都可以方便的触发 re-render。

benjycui avatar May 18 '17 06:05 benjycui

确认了,这个 API 没问题,是例子的写法有问题。

不过观点不变, 这个 API 的意义不大。

benjycui avatar May 18 '17 06:05 benjycui

除了 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

benjycui avatar May 18 '17 06:05 benjycui

Hello, i'm confused as to what is going on with this issue. Updated example still seems to be broken. Is there a workaround?

shulcsm avatar Jun 09 '17 15:06 shulcsm

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?

queimadus avatar Jul 20 '17 15:07 queimadus

Any news ?

pierre-H avatar Jan 16 '18 14:01 pierre-H

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.

XavierLeTohic avatar Jan 17 '18 13:01 XavierLeTohic

函数式组件如何 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

youngjuning avatar May 26 '20 08:05 youngjuning