-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix(@uform/react): invariant initialValues will not be changed when form rerender #214
fix(@uform/react): invariant initialValues will not be changed when form rerender #214
Conversation
packages/react/src/state/form.tsx
Outdated
@@ -182,17 +182,24 @@ export const StateForm = createHOC((options, Form) => { | |||
} | |||
|
|||
public componentDidUpdate(prevProps) { | |||
const { value, editable, initialValues } = this.props | |||
const { value, editable, initialValues, schema } = this.props | |||
const calculatedInitialValues = caculateSchemaInitialValues( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么只计算当前props的intialValues,不计算prevProps的intialValues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不用任何改动,仔细看一下,其实是props.initialValues引用被改动了,只需要在
https://github.com/alibaba/uform/blob/master/packages/core/src/index.ts#L30 加上clone即可,我们只需要每次对比的都是props就行了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
values 要不要也 clone 一份?
@@ -121,7 +121,7 @@ export const caculateSchemaInitialValues = ( | |||
if (!isEmpty(value)) { | |||
setIn(initialValues, name, value) | |||
} | |||
if (isFn(callback)) { | |||
if (callback && isFn(callback)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isFn
方法的通过不是确保了 callback
会是 Function
嘛,这样还需要再先确保 callback
是存在的吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果没有的话就不要执行 isFn 了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有道理😂😂
…hen form rerender
Closes #211
原因在于 initialValues 在 componentDidUpdate 的时候没有收集 schema 的 default,导致 initialValues 不一致。原因是 #214 (comment)