-
Notifications
You must be signed in to change notification settings - Fork 47k
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
useRef: Counting renders doesn´t work as expected in React neither 18.0.1 nor 18.0.2 #27880
Comments
The issue you're experiencing is due to the way React handles updates and re-renders. When your component first renders, To fix this, you need to ensure that First Method is using
Second method is using
|
@AliHaiderPgm thats a good thought, but the effect doesn't run until after render so it will show 0,1,2,3,4 as expected as written outside of strict mode. Your fix does solve the issue, because it skips the count for the first strict mode effect, but if this was run in prod it would miss the first render, since strict mode doesn't run in prod. The issue is that React 18 introduced a new feature for StrictMode that immediately unmounts and remounts effects to ensure they're properly cleaned up, to catch bugs like this. That's why it worked before 18 and doesn't work as expected. Check out this sandbox, and notice that is works if you remove from index.js: https://codesandbox.io/p/sandbox/blissful-darkness-xyz6m6?file=%2Fsrc%2Findex.js%3A10%2C18 The reason this is a bug is because when the component unmounts, you're not resetting the ref. That means when you use certain features, the ref won't be reset when you expect it to. For example, when Fast Refresh updates the component, you would expect the render count to reset to 0, but in your version the render count gets "stuck" at the current value and you need to hard refresh the page to reset it to 0. In future versions we plan to reset refs for you in strict mode to align with the production behavior (#25049), but hopefully this shows the value of the new strict mode behavior. Until that lands, to fix this, you can reset the ref when the component unmounts. You need two effects to do this because the effect that updates the count will destroy on every render, but you only want to reset on unmount: const ref = useRef(0);
useEffect(() => {
ref.current = ref.current + 1;
});
useEffect(() => {
return () => {
ref.current = 0;
};
}, []); Note: we also have a planned feature to support doing this in a single effect by allowing you to specify different functions that run for create vs update in the same effect: #25744 |
I am trying to count each instance I type a letter into an input. In React 17.0.2 works fine: 1,2,3,4,... . But in React 18.0.1 and 18.0.2 the counting starts from 2 jumping over the number 1: 2,3,4,...
React version: 18.0.1 and 18.0.2
Steps To Reproduce
Code:
The current behavior
2,3,4...
The expected behavior
1,2,3,4...
The text was updated successfully, but these errors were encountered: