question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global value is not updated inside of method

See original GitHub issue

image

I 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
CharlesStovercommented, May 8, 2019

To avoid the infinite loop, I think you would want something like:

if (computedIsMobile !== isMobile) {
  setIsMobile(computedIsMobile);
}

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.

0reactions
vladopaetscommented, May 8, 2019

Okay, first option creates infinite loop, but second one helped me! 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found