-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
FormSpy - Cannot update a component while rendering a different component #809
Comments
I am trying to read outside of the form the state saved in Redux and I got the same warning : Related issue: #828 Is there a workaround or am I stuck with it? |
The issue still persists with
I think the following happens: from the aspect of one particular field, registration indeed happens properly (first with the silent and then without the silent flag) but other fields could have completed this cycle already. So, when a field gets initialized it runs into the validation loop, causing the I have no suggestion at this point as to how it could be solved though. |
Does someone already have a solution or workaround for that problem? |
In my case this error was caused by a mutator, which was called on every I've solved this problem by wrapping the mutator call inside a |
Does anyone have an example solution to this? |
Hi all, I have been battling this issue while migrating from Redux-Form to React-Final-Form. Redux-Form has isDirty() in its API so I could easily get the state of a Form and use it in another (external) component. The only similar thing I can find for Final Form is FormSpy ( if not please let me know! ). In the end I had to give up and push the "dirty" flag into redux (in effect, rolling my own isDirty() for final-form) : which is when I bumped into the issue you are talking about here. So far the only way I can get rid of the Warning is by wrapping the redux // passed to FormSpy's onChange
onChange = ({dirty}) => {
// dodgy workaround to avoid Warning :P
setTimeout(()=>this.props.dispatch( formActions.changed({dirty}) ) ,0)
} |
I am experiencing the same issue in our project. |
@ThaNarie I hope you get rid of the error when you write it like this. this is how i solved it |
Could you please give an example? |
Seems like one of the solutions can be creating your own import { VFC, useEffect } from 'react'
import { useFormState } from 'react-final-form'
type Props = {
onChange: (props: any) => void
}
const component: VFC<Props> = ({ onChange }) => {
const state = useFormState()
useEffect(() => {
onChange(state)
})
return null
}
export default component |
Little react hook: don't have a real onChange handler until after the first render is called. function useAfterFirstRender(func) {
const [retFunc, setFunct] = useState(() => ()=>{});
useLayoutEffect(() => {
setFunct(() => func);
}, []);
return retFunc;
} |
Hi guys, As far as I understood, the issue is caused by |
Please see #985 (comment) |
That is a different issue |
…nal-form library. final-form/react-final-form#809 Due to inconsistent behavior from react-final-form resulting in the provision of old values, the Shipping component will apply the selected shipping method on mount. The call of handleShippingMethodChange(), which was previously used by FormSpy, will be performed when we click on the given radio button.
…nal-form library. final-form/react-final-form#809 Due to inconsistent behavior from react-final-form resulting in the provision of old values, the Shipping component will apply the selected shipping method on mount. The call of handleShippingMethodChange(), which was previously used by FormSpy, will be performed when we click on the given radio button.
…nal-form library. final-form/react-final-form#809 Due to inconsistent behavior from react-final-form resulting in the provision of old values, the Shipping component will apply the selected shipping method on mount. The call of handleShippingMethodChange(), which was previously used by FormSpy, will be performed when we click on the given radio button.
wrapping the callback in <FormSpy
subscription={{ values: true }}
onChange={(state) => {
requestAnimationFrame(() => {
// your callback body
});
}}
/> |
For me, creating a new component that only fires the event if the page has rendered did the job:
|
Are you submitting a bug report or a feature request?
bug report
What is the current behavior?
When using
FormSpy
, theonChange
is called during rendering, and throws an error if you usesetState
.What is the expected behavior?
No error.
Sandbox Link
https://codesandbox.io/s/react-final-form-formspy-hltnv?file=/src/App.js
What's your environment?
Other information
The issue seems to be that
onChange
in theuseFormState
is called in theuseState
init function, which means during rendering:react-final-form/src/useFormState.js
Line 26 in 464f1c7
This is part of a range of error messages introduced in react 16.13, and related to an existing issue (#751)
A workaround would be to put any
setState
on theonChange
in asetTimeout(... ,0)
, but this feels like a hack, and not clear to new users.The text was updated successfully, but these errors were encountered: