Global value is not updated inside of method
See original GitHub issueI have an issue that global value is not updated inside of method, check screenshot. I have initial value of isMobile: false
, then on line 21
I change it to true
, it is updated, but inside of handleResize
method it will be always false
. What could be wrong? cc @CharlesStover
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Global variable in function not updating - javascript
When you declare a variable inside a function scope, it shadows the outer ones no matter where you declare it (as the last...
Read more >Can you change a global variable's value from within a ...
I understand the idea that a local variable is independent of a global variable, and that they can both exist within the same...
Read more >Global Variable in Python With Examples [Updated]
A global variable in Python means having a scope throughout the program. Understand how to create and access global variables with examples.
Read more >Global variable not updating using useEffect() : r/reactjs
Updating a variable like that is the wrong approach. First, it won't cause a component to re-render. Only calling setState() or dispatch() will....
Read more >Update global var inside a function - JavaScript
The global var is being updated by a function triggered by an onclick() button event. I can't seem to update the var and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To avoid the infinite loop, I think you would want something like:
I think this is the most accurate solution.
Also, I think the
eslint-plugin-react-hooks
has a rule for this exact useEffect scenario.Will close this as resolved, but feel free to reopen it if you need further assistance.
Okay, first option creates infinite loop, but second one helped me! 👍