Skip to main content
22 events
when toggle format what by license comment
Dec 28, 2022 at 19:32 comment added Aprillion FTR setting state from inside a Promise chain started from useEffect or from somewhere else in context / parent / library / callback / stream / action creator / saga / store / init / SSR magic / wherever else, they all work the same in regads to setState vs the state value inside one render.
Dec 28, 2022 at 19:22 comment added Aprillion both answers address the implied question from the OP: Why do I see the previous value of state after I called setState from wherever? Feel free to use whatever pattern as explained in beta.reactjs.org or any library that might be useful in your project, I am not telling you what you should/n't do, just why the OP didn't work.
Dec 27, 2022 at 18:15 comment added ruffin "it wasn't meant as an advice to actually call setState from useEffect..." I'm sorry, I'm missing how this answers the OP then. That does seem to be the solution from the "previous answer" and this one, though it also seems to be anti-pattern -- you're using a mechanism for achieving side effects to change state. That is/n't antipattern & why?
Sep 23, 2022 at 2:39 comment added fawinell Yes! That's what I see from source code, the seState is behavior LIKE async, but IS sync exactly.
May 7, 2022 at 7:29 comment added Aprillion @MattFrei useEffect was only used to illustrate that setting the state happens after a render and the state value was remembered from the previous render, it wasn't meant as an advice to actually call setState from useEffect... the rest of the explanation should apply even for useState in a Context provider, but feel free to point me to a new SO question if additional deep dive would be useful
May 5, 2022 at 22:26 comment added Gabriel Oliveira excelent answer
Mar 15, 2022 at 6:52 comment added Matt Frei All of these solutions require the use of useEffect. My problem is that my "movies" equivalent is an object I get from a Context provider and can be changed by many other components. I don't want to run the effect every time it's changed because my effect isn't setMovies - it's a different function I need to call only when a particular change is made to movies - a change I'm not seeing when needed because of a stale context.
Nov 30, 2021 at 19:57 history edited Aprillion CC BY-SA 4.0
use suggestion from rolled-back edit as a separate solution, explain shortcommings of the ref solution
Nov 30, 2021 at 19:48 history rollback Aprillion
Rollback to Revision 8
Nov 29, 2021 at 16:56 history edited 3limin4t0r CC BY-SA 4.0
Replaced the "mutable reference" solution into one that uses a shadow variable. The main reason being: Changing a ref does not trigger a re-render—so although the value might be updated, the view is not. If you do not agree with this edit feel free to roll it back.
Feb 24, 2021 at 22:12 history edited Devon Bernard CC BY-SA 4.0
Fixed spelling mistakes
Oct 22, 2020 at 20:25 comment added Aprillion @ACV Solution 2 works fine for the original question. If you need to solve a different problem, YMMW, but I am still 100% sure that the quoted code works as documented and the problem is somewhere else.
Oct 22, 2020 at 13:57 comment added ACV for some reason solution 2 is not working.. I get the callback, but the value is still empty. useEffect(() => { console.log(movies) }, [movies]) this prints nothing ..
Jun 16, 2020 at 16:52 comment added Al Joslin actually I just finished a rewrite with useReducer, following @kentcdobs article (ref below) which really gave me a solid result that suffers not one bit from these closure problems. (ref: kentcdodds.com/blog/how-to-use-react-context-effectively)
Jun 16, 2020 at 7:59 comment added Aprillion @AlJoslin at a first glance, that seems like a separate problem, even if it might be caused by closure scope. If you have a concrete question, please create a new stackoverflow question with code example and all...
Jan 2, 2020 at 15:36 history edited Aprillion CC BY-SA 4.0
deleted 29 characters in body
Jan 2, 2020 at 15:30 history edited Aprillion CC BY-SA 4.0
add solutions for people looking for more than just explanation
Dec 31, 2019 at 10:22 history edited Aprillion CC BY-SA 4.0
lower case closure looks less scary
Nov 20, 2019 at 19:01 history edited Aprillion CC BY-SA 4.0
unnecessary iffe
Nov 20, 2019 at 18:54 history edited Aprillion CC BY-SA 4.0
Better synchronous example
Nov 15, 2019 at 13:23 history edited Aprillion CC BY-SA 4.0
added 9 characters in body
Nov 15, 2019 at 13:16 history answered Aprillion CC BY-SA 4.0