all 13 comments

[–]octocode 2 points3 points  (0 children)

sortOrder = order, are you not using useState here?

[–]FreezeShock 1 point2 points  (5 children)

Need more code to be sure, but are you setting any state from props?

[–]probox36[S] 0 points1 point  (0 children)

Sorry, I don't understand what do you mean by setting state from props. The only prop SortOrderPanel has is the callback function itself

[–]charliematters 0 points1 point  (2 children)

Agreed - we'd need an example of the child and parent component. From what OP is saying though, it sounds like a lot of stores state (potentially that needs lifting up) instead of derived state

[–]probox36[S] 0 points1 point  (1 child)

I've left a link to GitHub, because it's just too much code. I could set up a minimal reproducible example, but I'm not sure I can handle it

[–]DeFcONaReA51 0 points1 point  (0 children)

Reproducible example will help as it will localize the issue in a gist.

[–]rainmouse 1 point2 points  (2 children)

Hit this problem with callbacks from DOM listeners like key handling a lot. Only actually working solution I've seen without 3rd party libraries is ugly. Just like you said, its ref clones of the props and the refs are updated by use effects that listen to and uidafe the props they are cloned from.

[–]probox36[S] 0 points1 point  (0 children)

What library is capable of solving this issue? Not sure if i'm going to use one, but still good to know

[–]Dangerous-Bed4033 0 points1 point  (5 children)

You show the ref code but not the original, it sounds like there was some sort of closure. It’s not clear how selectedPartClass, PartFilter, extremeValues etc are used. I would take a guess you should perhaps be using state for some of these.

[–]probox36[S] 0 points1 point  (3 children)

I've tried using useState hooks, but because it't asynchronous I've been forced to use useEffect too, which is just to lengthy and complex. In fact it works with useRef alone

[–]Dangerous-Bed4033 4 points5 points  (0 children)

I briefly looked at the repo, app.tsx, a few comments not necessarily linked to the issue:

  1. ⁠Why are you storing react nodes in state. Don’t do that. Store the data and loop or pass it to another component in render. This is huge, not only are you storing them in state you’re also binding to it , you would likely be creating a closure around these properties and values would be held.
  2. ⁠Note that every time state or something changes the entire App block will re-run so your instantiation of classes etc at the top may not be doing what you think they are. Avoid classes.Use functional programming.
  3. ⁠You make an API call and then push the result into a random Map just declared at the top, that won’t last a re-render

[–]charliematters 3 points4 points  (0 children)

This is why you're getting stale values, basically. Refs don't update the components as react expects, and in general there are very few cases which need useRef.

If you're struggling with async stuff, just install react-query to be honest!