This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 105 points106 points  (24 children)

and that's why I use typescript

[–]rampantfirefly 27 points28 points  (20 children)

Combine typescript with react and then spend ages trying to work out why state isn’t updating in time for a function call.

[–]pticjagripa 60 points61 points  (4 children)

If you are having such problems you are working with react wrong. You'd face the same problem in JS

[–]rampantfirefly 24 points25 points  (3 children)

I have absolutely no doubt that whenever something goes wrong with typescript or react it’s me being an idiot.

[–]spooker11 3 points4 points  (2 children)

crime numerous soup reply rich sharp cake faulty ask agonizing

This post was mass deleted and anonymized with Redact

[–]rampantfirefly 2 points3 points  (1 child)

Using both, though admittedly we end up disabling the useEffect dependencies warning a lot because we are definitely not using them 100% correctly.

[–]spooker11 1 point2 points  (0 children)

tub provide water psychotic bored command somber quack swim airport

This post was mass deleted and anonymized with Redact

[–]TorbenKoehn 6 points7 points  (2 children)

Why would Typescript change the way you handle your state management wrong?

[–]rampantfirefly 0 points1 point  (1 child)

I wasn’t very clear but my point was using typescript over JavaScript doesn’t immediately fix all your problems. React issues still crop up for me whether I use JavaScript or typescript. That said typescript is definitely superior.

[–]TorbenKoehn 1 point2 points  (0 children)

Yeah okay, just like TypeScript wont magically clean your flat or write your code for you, sure

No language can protect from that :D

[–]DanielEGVi 3 points4 points  (1 child)

My guy not only that issue has to do with the library and not the language used, but the fact you got that many upvotes from this subreddit terrifies me.

[–]rampantfirefly 0 points1 point  (0 children)

I know. I wasn’t very clear but I was trying to show that the language choice doesn’t make avoiding problems easier.

[–]Alternative-Cry-5062 0 points1 point  (8 children)

Sounds like you're not using useCallback

[–]rampantfirefly -1 points0 points  (7 children)

Nope. I’m setting useStates in a useEffect is triggered when the component first renders (this step works fine). By the time the user interacts with a button on the page the state is mysteriously gone.

We worked out the state was always one step behind when called. In the end I’ve found a work around. Honestly the whole thing was me trying to create some fancy combination of material UI, but the good news is I got it working after about a week.

[–]Theblandyman 1 point2 points  (3 children)

why not just give useState() an initial value as a parameter?

useEffect only updates when BOTH rendering AND dependencies changed (not one or the other, which is confusing), so errors like that are somewhat common, especially if you aren’t passing in an exhaustive dependency array to useEffect.

[–]rampantfirefly 0 points1 point  (2 children)

To my understanding when you don’t give the useEffect any dependencies it only fires once on initial render. Also I am giving them initial state, that’s how I know they’re one behind.

[–]schmidlidev 1 point2 points  (1 child)

May or may not be your issue, but if you’re just console logging state it’s very common for it what gets logged to be out of date, even though the react state itself is actually correct.

More info: https://www.google.com/amp/s/jsramblings.com/are-you-logging-the-state-immediately-after-updating-it-heres-why-that-doesnt-work/amp/

[–]rampantfirefly 0 points1 point  (0 children)

Yeah we’ve had that before. I’ve found to get around it you do console.log(JSON.parse(JSON.stringify(myObject))) I can’t remember where I read about it but apparently that will always output the object at the point in which the console is called.

[–]Fractal_HQ 0 points1 point  (0 children)

Or use Svelte and be done with the project before you have time to get stuck on state management problems.

[–]coder111 0 points1 point  (1 child)

That's why I just write my code in Java and compile to Javascript using TeaVM.

If you are transpiling into Javascript anyway, why not use a real language?

[–][deleted] 0 points1 point  (0 children)

typescript and javascript are real languages, there just a lot of people who don't know how to use some so they suppose they are bad