Skip to content
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

[Beta] useState Troubleshooting #4309

Merged
merged 3 commits into from
Feb 7, 2022
Merged

[Beta] useState Troubleshooting #4309

merged 3 commits into from
Feb 7, 2022

Conversation

gaearon
Copy link
Member

@gaearon gaearon commented Feb 7, 2022

Preview page

Adds the most common mistakes and common solutions. I tried to keep it light and to-the-point.

Also added a bit of extra content to the earlier example gallery.

@github-actions
Copy link

github-actions bot commented Feb 7, 2022

Size Changes

📦 Next.js Bundle Analysis

This analysis was generated by the next.js bundle analysis action 🤖

One Hundred Eighty-three Pages Changed Size

The following pages changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load
/ 217.86 KB (🟡 +4 B) 311.06 KB
/404 216.97 KB (🟡 +4 B) 310.17 KB
/apis 217.51 KB (🟡 +4 B) 310.71 KB
/apis/reactdom 217.65 KB (🟡 +4 B) 310.86 KB
/apis/render 219 KB (🟡 +4 B) 312.2 KB
/apis/usestate 227.02 KB (🟡 +2.08 KB) 320.22 KB
/blog/2013/06/02/jsfiddle-integration 228.24 KB (🟡 +4 B) 321.44 KB
/blog/2013/06/05/why-react 229.96 KB (🟡 +4 B) 323.16 KB
/blog/2013/06/12/community-roundup 229.67 KB (🟡 +4 B) 322.87 KB
/blog/2013/06/19/community-roundup-2 230.33 KB (🟡 +4 B) 323.54 KB
/blog/2013/06/21/react-v0-3-3 228.38 KB (🟡 +4 B) 321.59 KB
/blog/2013/06/27/community-roundup-3 230.7 KB (🟡 +4 B) 323.9 KB
/blog/2013/07/02/react-v0-4-autobind-by-default 229 KB (🟡 +4 B) 322.21 KB
/blog/2013/07/03/community-roundup-4 230.07 KB (🟡 +4 B) 323.27 KB
/blog/2013/07/11/react-v0-4-prop-validation-and-default-values 228.87 KB (🟡 +4 B) 322.08 KB
/blog/2013/07/17/react-v0-4-0 229.58 KB (🟡 +4 B) 322.79 KB
/blog/2013/07/23/community-roundup-5 230.46 KB (🟡 +4 B) 323.67 KB
/blog/2013/07/26/react-v0-4-1 228.35 KB (🟡 +4 B) 321.55 KB
/blog/2013/07/30/use-react-and-jsx-in-ruby-on-rails 228.95 KB (🟡 +4 B) 322.15 KB
/blog/2013/08/05/community-roundup-6 229.75 KB (🟡 +4 B) 322.96 KB
/blog/2013/08/19/use-react-and-jsx-in-python-applications 228.96 KB (🟡 +4 B) 322.17 KB
/blog/2013/08/26/community-roundup-7 229.93 KB (🟡 +4 B) 323.13 KB
/blog/2013/09/24/community-roundup-8 231.36 KB (🟡 +4 B) 324.56 KB
/blog/2013/10/03/community-roundup-9 230.29 KB (🟡 +4 B) 323.5 KB
/blog/2013/10/16/react-v0.5.0 229.98 KB (🟡 +4 B) 323.18 KB
/blog/2013/10/29/react-v0-5-1 228.34 KB (🟡 +4 B) 321.54 KB
/blog/2013/11/06/community-roundup-10 231.96 KB (🟡 +4 B) 325.16 KB
/blog/2013/11/18/community-roundup-11 231.03 KB (🟡 +4 B) 324.24 KB
/blog/2013/12/18/react-v0.5.2-v0.4.2 228.81 KB (🟡 +4 B) 322.01 KB
/blog/2013/12/19/react-v0.8.0 229.23 KB (🟡 +4 B) 322.43 KB
/blog/2013/12/23/community-roundup-12 230.69 KB (🟡 +4 B) 323.89 KB
/blog/2013/12/30/community-roundup-13 230.47 KB (🟡 +4 B) 323.67 KB
/blog/2014/01/02/react-chrome-developer-tools 228.73 KB (🟡 +4 B) 321.93 KB
/blog/2014/01/06/community-roundup-14 230.07 KB (🟡 +4 B) 323.27 KB
/blog/2014/02/05/community-roundup-15 231.28 KB (🟡 +4 B) 324.49 KB
/blog/2014/02/15/community-roundup-16 230.99 KB (🟡 +4 B) 324.2 KB
/blog/2014/02/16/react-v0.9-rc1 231.4 KB (🟡 +4 B) 324.6 KB
/blog/2014/02/20/react-v0.9 231.84 KB (🟡 +4 B) 325.05 KB
/blog/2014/02/24/community-roundup-17 230.71 KB (🟡 +4 B) 323.91 KB
/blog/2014/03/14/community-roundup-18 231.67 KB (🟡 +4 B) 324.88 KB
/blog/2014/03/19/react-v0.10-rc1 229.98 KB (🟡 +4 B) 323.18 KB
/blog/2014/03/21/react-v0.10 230.01 KB (🟡 +4 B) 323.21 KB
/blog/2014/03/28/the-road-to-1.0 230.12 KB (🟡 +4 B) 323.32 KB
/blog/2014/04/04/reactnet 228.76 KB (🟡 +4 B) 321.96 KB
/blog/2014/05/06/flux 228.88 KB (🟡 +4 B) 322.09 KB
/blog/2014/05/29/one-year-of-open-source-react 229.11 KB (🟡 +4 B) 322.31 KB
/blog/2014/06/27/community-roundup-19 230.65 KB (🟡 +4 B) 323.85 KB
/blog/2014/07/13/react-v0.11-rc1 231.04 KB (🟡 +4 B) 324.24 KB
/blog/2014/07/17/react-v0.11 232.3 KB (🟡 +4 B) 325.5 KB
/blog/2014/07/25/react-v0.11.1 229.24 KB (🟡 +4 B) 322.45 KB
/blog/2014/07/28/community-roundup-20 230.8 KB (🟡 +4 B) 324 KB
/blog/2014/07/30/flux-actions-and-the-dispatcher 230.43 KB (🟡 +4 B) 323.63 KB
/blog/2014/08/03/community-roundup-21 230.46 KB (🟡 +4 B) 323.67 KB
/blog/2014/09/03/introducing-the-jsx-specification 228.47 KB (🟡 +4 B) 321.67 KB
/blog/2014/09/12/community-round-up-22 230.95 KB (🟡 +4 B) 324.15 KB
/blog/2014/09/16/react-v0.11.2 229.24 KB (🟡 +4 B) 322.44 KB
/blog/2014/09/24/testing-flux-applications 232.42 KB (🟡 +4 B) 325.62 KB
/blog/2014/10/14/introducing-react-elements 231.38 KB (🟡 +4 B) 324.58 KB
/blog/2014/10/16/react-v0.12-rc1 231.34 KB (🟡 +4 B) 324.54 KB
/blog/2014/10/17/community-roundup-23 231.91 KB (🟡 +4 B) 325.12 KB
/blog/2014/10/27/react-js-conf 228.51 KB (🟡 +4 B) 321.72 KB
/blog/2014/10/28/react-v0.12 231.28 KB (🟡 +4 B) 324.48 KB
/blog/2014/11/24/react-js-conf-updates 228.9 KB (🟡 +4 B) 322.1 KB
/blog/2014/11/25/community-roundup-24 232.14 KB (🟡 +4 B) 325.34 KB
/blog/2014/12/18/react-v0.12.2 228.93 KB (🟡 +4 B) 322.13 KB
/blog/2014/12/19/react-js-conf-diversity-scholarship 229.32 KB (🟡 +4 B) 322.53 KB
/blog/2015/01/27/react-v0.13.0-beta-1 230.29 KB (🟡 +4 B) 323.49 KB
/blog/2015/02/18/react-conf-roundup-2015 232.28 KB (🟡 +4 B) 325.48 KB
/blog/2015/02/20/introducing-relay-and-graphql 231.43 KB (🟡 +4 B) 324.63 KB
/blog/2015/02/24/react-v0.13-rc1 230.33 KB (🟡 +4 B) 323.53 KB
/blog/2015/02/24/streamlining-react-elements 232.88 KB (🟡 +4 B) 326.08 KB
/blog/2015/03/03/react-v0.13-rc2 229.67 KB (🟡 +4 B) 322.87 KB
/blog/2015/03/04/community-roundup-25 230.63 KB (🟡 +4 B) 323.83 KB
/blog/2015/03/10/react-v0.13 230.95 KB (🟡 +4 B) 324.15 KB
/blog/2015/03/16/react-v0.13.1 228.76 KB (🟡 +4 B) 321.96 KB
/blog/2015/03/19/building-the-facebook-news-feed-with-relay 231.52 KB (🟡 +4 B) 324.72 KB
/blog/2015/03/26/introducing-react-native 228.66 KB (🟡 +4 B) 321.86 KB
/blog/2015/03/30/community-roundup-26 230.59 KB (🟡 +4 B) 323.79 KB
/blog/2015/04/17/react-native-v0.4 229.51 KB (🟡 +4 B) 322.71 KB
/blog/2015/04/18/react-v0.13.2 228.84 KB (🟡 +4 B) 322.04 KB
/blog/2015/05/01/graphql-introduction 233.33 KB (🟡 +4 B) 326.53 KB
/blog/2015/05/08/react-v0.13.3 228.73 KB (🟡 +4 B) 321.93 KB
/blog/2015/05/22/react-native-release-process 228.88 KB (🟡 +4 B) 322.08 KB
/blog/2015/06/12/deprecating-jstransform-and-react-tools 229.33 KB (🟡 +4 B) 322.54 KB
/blog/2015/07/03/react-v0.14-beta-1 230.85 KB (🟡 +4 B) 324.05 KB
/blog/2015/08/03/new-react-devtools-beta 229.45 KB (🟡 +4 B) 322.65 KB
/blog/2015/08/11/relay-technical-preview 229.51 KB (🟡 +4 B) 322.71 KB
/blog/2015/08/13/reacteurope-roundup 231.1 KB (🟡 +4 B) 324.3 KB
/blog/2015/09/02/new-react-developer-tools 228.86 KB (🟡 +4 B) 322.07 KB
/blog/2015/09/10/react-v0.14-rc1 234.55 KB (🟡 +4 B) 327.75 KB
/blog/2015/09/14/community-roundup-27 231.02 KB (🟡 +4 B) 324.22 KB
/blog/2015/10/01/react-render-and-top-level-api 230 KB (🟡 +4 B) 323.2 KB
/blog/2015/10/07/react-v0.14 235.03 KB (🟡 +4 B) 328.24 KB
/blog/2015/10/19/reactiflux-is-moving-to-discord 230.75 KB (🟡 +4 B) 323.95 KB
/blog/2015/10/28/react-v0.14.1 228.75 KB (🟡 +4 B) 321.96 KB
/blog/2015/11/02/react-v0.14.2 228.78 KB (🟡 +4 B) 321.99 KB
/blog/2015/11/18/react-v0.14.3 228.91 KB (🟡 +4 B) 322.11 KB
/blog/2015/12/04/react-js-conf-2016-diversity-scholarship 229.78 KB (🟡 +4 B) 322.98 KB
/blog/2015/12/16/ismounted-antipattern 229.38 KB (🟡 +4 B) 322.58 KB
/blog/2015/12/18/react-components-elements-and-instances 233.3 KB (🟡 +4 B) 326.51 KB
/blog/2015/12/29/react-v0.14.4 228.58 KB (🟡 +4 B) 321.78 KB
/blog/2016/01/08/A-implies-B-does-not-imply-B-implies-A 229.59 KB (🟡 +4 B) 322.8 KB
/blog/2016/01/12/discontinuing-ie8-support 228.4 KB (🟡 +4 B) 321.6 KB
/blog/2016/02/19/new-versioning-scheme 229.78 KB (🟡 +4 B) 322.98 KB
/blog/2016/03/07/react-v15-rc1 232.53 KB (🟡 +4 B) 325.74 KB
/blog/2016/03/16/react-v15-rc2 229.4 KB (🟡 +4 B) 322.6 KB
/blog/2016/03/29/react-v0.14.8 228.53 KB (🟡 +4 B) 321.73 KB
/blog/2016/04/07/react-v15 236.72 KB (🟡 +4 B) 329.92 KB
/blog/2016/04/08/react-v15.0.1 229.29 KB (🟡 +4 B) 322.49 KB
/blog/2016/07/11/introducing-reacts-error-code-system 229 KB (🟡 +4 B) 322.2 KB
/blog/2016/07/13/mixins-considered-harmful 237.15 KB (🟡 +4 B) 330.36 KB
/blog/2016/07/22/create-apps-with-no-configuration 232.48 KB (🟡 +4 B) 325.68 KB
/blog/2016/08/05/relay-state-of-the-state 232.52 KB (🟡 +4 B) 325.72 KB
/blog/2016/09/28/our-first-50000-stars 233.97 KB (🟡 +4 B) 327.17 KB
/blog/2016/11/16/react-v15.4.0 231.92 KB (🟡 +4 B) 325.13 KB
/blog/2017/04/07/react-v15.5.0 232.49 KB (🟡 +4 B) 325.69 KB
/blog/2017/05/18/whats-new-in-create-react-app 231.88 KB (🟡 +4 B) 325.08 KB
/blog/2017/06/13/react-v15.6.0 230.5 KB (🟡 +4 B) 323.71 KB
/blog/2017/07/26/error-handling-in-react-16 230.85 KB (🟡 +4 B) 324.06 KB
/blog/2017/09/08/dom-attributes-in-react-16 231.28 KB (🟡 +4 B) 324.49 KB
/blog/2017/09/25/react-v15.6.2 229.62 KB (🟡 +4 B) 322.82 KB
/blog/2017/09/26/react-v16.0 234.75 KB (🟡 +4 B) 327.95 KB
/blog/2017/11/28/react-v16.2.0-fragment-support 232.66 KB (🟡 +4 B) 325.86 KB
/blog/2017/12/07/introducing-the-react-rfc-process 229.2 KB (🟡 +4 B) 322.4 KB
/blog/2017/12/15/improving-the-repository-infrastructure 244.99 KB (🟡 +4 B) 338.19 KB
/blog/2018/03/01/sneak-peek-beyond-react-16 229.16 KB (🟡 +4 B) 322.36 KB
/blog/2018/03/27/update-on-async-rendering 234.4 KB (🟡 +4 B) 327.6 KB
/blog/2018/03/29/react-v-16-3 231.22 KB (🟡 +4 B) 324.42 KB
/blog/2018/05/23/react-v-16-4 231.36 KB (🟡 +4 B) 324.56 KB
/blog/2018/06/07/you-probably-dont-need-derived-state 235.02 KB (🟡 +4 B) 328.22 KB
/blog/2018/08/01/react-v-16-4-2 230.01 KB (🟡 +4 B) 323.21 KB
/blog/2018/09/10/introducing-the-react-profiler 231.62 KB (🟡 +4 B) 324.82 KB
/blog/2018/10/01/create-react-app-v2 232.1 KB (🟡 +4 B) 325.3 KB
/blog/2018/10/23/react-v-16-6 231.3 KB (🟡 +4 B) 324.5 KB
/blog/2018/11/13/react-conf-recap 229.01 KB (🟡 +4 B) 322.21 KB
/blog/2018/11/27/react-16-roadmap 235.2 KB (🟡 +4 B) 328.4 KB
/blog/2018/12/19/react-v-16-7 230.13 KB (🟡 +4 B) 323.33 KB
/blog/2019/02/06/react-v16.8.0 232.4 KB (🟡 +4 B) 325.6 KB
/blog/2019/02/23/is-react-translated-yet 231.96 KB (🟡 +4 B) 325.16 KB
/blog/2019/08/08/react-v16.9.0 234.89 KB (🟡 +4 B) 328.1 KB
/blog/2019/08/15/new-react-devtools 229.5 KB (🟡 +4 B) 322.7 KB
/blog/2019/10/22/react-release-channels 231.31 KB (🟡 +4 B) 324.51 KB
/blog/2019/11/06/building-great-user-experiences-with-concurrent-mode-and-suspense 236.01 KB (🟡 +4 B) 329.22 KB
/blog/2020/02/26/react-v16.13.0 232.24 KB (🟡 +4 B) 325.44 KB
/blog/2020/08/10/react-v17-rc 238.52 KB (🟡 +4 B) 331.72 KB
/community 217.55 KB (🟡 +4 B) 310.75 KB
/community/acknowledgements 218.75 KB (🟡 +4 B) 311.95 KB
/community/meet-the-team 219.26 KB (🟡 +4 B) 312.47 KB
/learn 222.05 KB (🟡 +4 B) 315.25 KB
/learn/add-react-to-a-website 222.38 KB (🟡 +4 B) 315.58 KB
/learn/adding-interactivity 224.18 KB (🟡 +6 B) 317.39 KB
/learn/choosing-the-state-structure 229.92 KB (🟡 +4 B) 323.12 KB
/learn/conditional-rendering 222.15 KB (🟡 +4 B) 315.35 KB
/learn/describing-the-ui 221.37 KB (🟡 +4 B) 314.57 KB
/learn/editor-setup 218.64 KB (🟡 +4 B) 311.84 KB
/learn/escape-hatches 216.88 KB (🟡 +4 B) 310.09 KB
/learn/extracting-state-logic-into-a-reducer 227.52 KB (🟡 +4 B) 320.72 KB
/learn/importing-and-exporting-components 220.55 KB (🟡 +4 B) 313.75 KB
/learn/installation 218.21 KB (🟡 +4 B) 311.41 KB
/learn/javascript-in-jsx-with-curly-braces 220.63 KB (🟡 +4 B) 313.83 KB
/learn/keeping-components-pure 224.76 KB (🟡 +4 B) 317.96 KB
/learn/managing-state 223.48 KB (🟡 +4 B) 316.69 KB
/learn/manipulating-the-dom-with-refs 225.39 KB (🟡 +4 B) 318.59 KB
/learn/passing-data-deeply-with-context 224.91 KB (🟡 +4 B) 318.11 KB
/learn/passing-props-to-a-component 224.33 KB (🟡 +4 B) 317.53 KB
/learn/preserving-and-resetting-state 227.14 KB (🟡 +4 B) 320.34 KB
/learn/queueing-a-series-of-state-updates 222.12 KB (🟡 +4 B) 315.33 KB
/learn/react-developer-tools 218.08 KB (🟡 +4 B) 311.28 KB
/learn/reacting-to-input-with-state 226.6 KB (🟡 +4 B) 319.8 KB
/learn/referencing-values-with-refs 223.64 KB (🟡 +4 B) 316.84 KB
/learn/render-and-commit 220.61 KB (🟡 +4 B) 313.82 KB
/learn/rendering-lists 224.68 KB (🟡 +4 B) 317.88 KB
/learn/responding-to-events 223.67 KB (🟡 +4 B) 316.87 KB
/learn/scaling-up-with-reducer-and-context 221.7 KB (🟡 +4 B) 314.9 KB
/learn/sharing-state-between-components 222.4 KB (🟡 +4 B) 315.61 KB
/learn/start-a-new-react-project 219.05 KB (🟡 +4 B) 312.25 KB
/learn/state-a-components-memory 227.85 KB (🟡 +4 B) 321.05 KB
/learn/state-as-a-snapshot 221.67 KB (🟡 +4 B) 314.87 KB
/learn/thinking-in-react 223.49 KB (🟡 +4 B) 316.69 KB
/learn/updating-arrays-in-state 225.73 KB (🟡 +4 B) 318.93 KB
/learn/updating-objects-in-state 225.45 KB (🟡 +4 B) 318.65 KB
/learn/writing-markup-with-jsx 220.55 KB (🟡 +4 B) 313.75 KB
/learn/your-first-component 221.45 KB (🟡 +4 B) 314.65 KB
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 10% or more, there will be a red status indicator applied, indicating that special attention should be given to this.

}
```

This is because [states behaves like a snapshot](/learn/state-as-a-snapshot). Updating state requests another render with the new state value, but does not affect the `count` JavaScript variable in your already running event handler.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is because [states behaves like a snapshot](/learn/state-as-a-snapshot). Updating state requests another render with the new state value, but does not affect the `count` JavaScript variable in your already running event handler.
This is because [states behaves like a snapshot](/learn/state-as-a-snapshot). Updating state requests another render with the new state value, but does not affect the `count` JavaScript variable in your already-running event handler.

return <button onClick={(e) => handleClick(e)}>Click me</button>
```

If you can't find the cause of this error, click on the arrow next to the error in the console, and look through the JavaScript stack to find the specific `set` function call responsible for the error.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this tip. Do you think we could add an example of this (a screenshot, for instance)?

Also, an unnecessary comma:

Suggested change
If you can't find the cause of this error, click on the arrow next to the error in the console, and look through the JavaScript stack to find the specific `set` function call responsible for the error.
If you can't find the cause of this error, click on the arrow next to the error in the console and look through the JavaScript stack to find the specific `set` function call responsible for the error.

BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
 (reactjs#721)

* Sandpack error icon overlapping issue fix (reactjs#4302)

* sandpack error icon overlapping issue fix

* modified errorline css

* Improve font display (reactjs#4308)

* Generate Ids when there are none in local development (reactjs#4304)

* Generate Ids when there are no headings

* Tweak code

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>

* [Beta] useState Troubleshooting (reactjs#4309)

* [Beta] useState Troubleshooting

* Tweaks

* tweak

* docs: phrasing a sentence (reactjs#4185)

* docs: phrasing a sentence (reactjs#4182)

* docs: phrasing a sentence

* Update extracting-state-logic-into-a-reducer.md

Co-authored-by: dan <dan.abramov@gmail.com>

* docs: fix a grammatical error (reactjs#4183)

Co-authored-by: dan <dan.abramov@gmail.com>

* Change "return statement" to "return keyword" (reactjs#4137)

* small fixes to stopwatch codesandbox (reactjs#4110)

* small fixes to stopwatch codesandbox

noticed that the explanation for the first stopwatch codesandbox mentions "update the time every 10 milliseconds" so updated the codesandbox to reflect that

also there's a small nuanced bug in the second stopwatch codesandbox where each call to `handleStart()` sets a new interval without checking if there's already one ongoing. 

Ie: If the user accidentally double clicks the start button, they set two intervals for updating `now` every 10ms and then intervalRef only retains the second interval ID. Thus, it's impossible to actually stop the timer because `handleStop()` will only clear the latest set interval while the original one will keep executing.

* Update referencing-values-with-refs.md

* Update referencing-values-with-refs.md

* Update referencing-values-with-refs.md

Co-authored-by: dan <dan.abramov@gmail.com>

* Resolve conflicts

Co-authored-by: Amaresh  S M <amaresh.suriya@freshworks.com>
Co-authored-by: Sha Mwe La <62544170+shamwela@users.noreply.github.com>
Co-authored-by: Strek <harish.sethuraman@freshworks.com>
Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
Co-authored-by: Sofya Tuymedova <stuymedova@gmail.com>
Co-authored-by: Soichiro Miki <smiki-tky@umin.ac.jp>
Co-authored-by: Aayush Kumar <aayush.kumarmail@gmail.com>
Co-authored-by: KnowsCount <knowscount@gmail.com>
@rickhanlonii rickhanlonii deleted the usestate-troubleshooting branch April 29, 2024 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants