You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: FunctionComponent re-render phase cause a bug #20675
The first time click the button, it will not print the "effect"
But the second click will print the "effect"
the third will not, the fourth will be and so on
The expected behavior
Maybe should always print nothing!
Remarks
The reason for this phenomenon is when component entering re-render phase, the effect object is a big different in updateQueue and Hook object. But i think should be consistent。
In fact, i am not sure if it is a bug, so I look forward to receiving a reply, thanks
it('yields inconsistently when triggering state updates during render',()=>{lethandleClick;functionTest(){const[count,setCount]=useState(0);useEffect(()=>{Scheduler.unstable_yieldValue(`Effect: ${count}`);},[count]);if(count>0){setCount(0);}handleClick=()=>setCount(2);return<Texttext={`Render: ${count}`}/>;}ReactNoop.act(()=>{ReactNoop.render(<Test/>);});expect(Scheduler).toHaveYielded(['Render: 0','Effect: 0']);ReactNoop.act(()=>{handleClick();});expect(Scheduler).toHaveYielded(['Render: 0']);ReactNoop.act(()=>{handleClick();});expect(Scheduler).toHaveYielded(['Render: 0','Effect: 0']);ReactNoop.act(()=>{handleClick();});expect(Scheduler).toHaveYielded(['Render: 0']);});
I would've expected expect(Scheduler).toHaveYielded(['Render: 0', 'Effect: 0']); to pass after every handleClick but every second click only yields ['Render: 0'] Not sure why I expected this. We only commit with unchanged deps so we should never run the effect. Yielding on every click with ['Render: 0'] is the correct answer.
I tried it in v17.0.1 and this problem still occurs, because the function updateEffectImpl has not changed.
Could you either update the issue or the codesandbox so that we avoid potential confusion when the bug is reported for one version but the repro uses a different one?
Could you either update the issue or the codesandbox so that we avoid potential confusion when the bug is reported for one version but the repro uses a different one?
yeah! i have updated the version of React in the codesandbox
Activity
[-]Bug: [/-][+]Bug: FunctionComponent re-render phase cause a bug[/+]eps1lon commentedon Jan 27, 2021
Thanks for the report.
The codesandbox is using
0.0.0-experimental-4ead6b530. Could you clarify for which version you want to report the bug?careyke commentedon Jan 27, 2021
I tried it in v17.0.1 and this problem still occurs, because the function
updateEffectImplhas not changed.eps1lon commentedon Jan 27, 2021
Test illustrating the current behavior on
master:I would've expectedNot sure why I expected this. We only commit with unchanged deps so we should never run the effect. Yielding on every click withexpect(Scheduler).toHaveYielded(['Render: 0', 'Effect: 0']);to pass after everyhandleClickbut every second click only yields['Render: 0']['Render: 0']is the correct answer.Could you either update the issue or the codesandbox so that we avoid potential confusion when the bug is reported for one version but the repro uses a different one?
careyke commentedon Jan 27, 2021
yeah! i have updated the version of React in the codesandbox
gaearon commentedon Jan 27, 2021
Is the behavior new in some version, or always been like this?
eps1lon commentedon Jan 27, 2021
It always has been™. Tested with a codesandbox using 16.8.
eps1lon commentedon Jan 29, 2021
@careyke Thanks for the repro! This helped a lot identifying the issue.
careyke commentedon Jan 30, 2021
You are welcome! i am very happy to do this