@@ -1078,3 +1078,47 @@ describe("Issue #914 – nested Field with validator causing Maximum update dept
10781078 } ) . not . toThrow ( ) ;
10791079 } ) ;
10801080} ) ;
1081+
1082+ describe ( "Issue #850 – Cannot update component while rendering different component" , ( ) => {
1083+ it ( "should not warn about updating during render when conditionally rendering Field with initialValue" , async ( ) => {
1084+ // https://github.com/final-form/react-final-form/issues/850
1085+ const warnSpy = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
1086+ const Toggle = ( { children } ) => {
1087+ const [ show , setShow ] = React . useState ( false ) ;
1088+ return (
1089+ < div >
1090+ < button onClick = { ( ) => setShow ( ( s ) => ! s ) } > Toggle</ button >
1091+ { show && children }
1092+ </ div >
1093+ ) ;
1094+ } ;
1095+
1096+ const { getByText } = render (
1097+ < Form onSubmit = { onSubmitMock } subscription = { { } } >
1098+ { ( { handleSubmit } ) => (
1099+ < form onSubmit = { handleSubmit } >
1100+ < Field name = "name" component = "input" initialValue = "erik" />
1101+ < Toggle >
1102+ < Field name = "message" component = "input" initialValue = "hello" />
1103+ </ Toggle >
1104+ </ form >
1105+ ) }
1106+ </ Form > ,
1107+ ) ;
1108+
1109+ // Toggle the conditional field
1110+ await act ( async ( ) => {
1111+ fireEvent . click ( getByText ( "Toggle" ) ) ;
1112+ } ) ;
1113+
1114+ // Should not have any "Cannot update component while rendering" warnings
1115+ const updateWhileRenderingWarnings = warnSpy . mock . calls . filter (
1116+ ( call ) =>
1117+ call [ 0 ] &&
1118+ typeof call [ 0 ] === "string" &&
1119+ call [ 0 ] . includes ( "Cannot update" ) ,
1120+ ) ;
1121+ expect ( updateWhileRenderingWarnings ) . toHaveLength ( 0 ) ;
1122+ warnSpy . mockRestore ( ) ;
1123+ } ) ;
1124+ } ) ;
0 commit comments