News for hackers. Stuff that matters.
Re:Persisting React State in LocalStorage by ipsum2 on Friday February 28, 2020 @12:07PM (#22444234)
Heads up to the author, the code example is wrong.

  function useStickyState(defaultValue, key)
should be

  function useStickyState(key, defaultValue)
Re:Persisting React State in LocalStorage by XCSme on Friday February 28, 2020 @01:11PM (#22444882)
I was also very confused by this. Does anyone know why this article has so many votes? This is a trivial solution and there are many other articles explaining this exact thing.
Re:Persisting React State in LocalStorage by dinkleberg on Friday February 28, 2020 @01:54PM (#22445262)
Because it brings attention to an interesting concept in a simple way. I'm starting to use React again after a year away using Vue and I've only just started to play with hooks. So it's cool to see other use cases.

And even if it is a trivial solution, it brings up an interesting idea. Why this article and not the others? Because it was posted at the right time. I'm sure if another one of those articles was posted here (and was written in an equally approachable way) it would do just as well.

Re:Persisting React State in LocalStorage by XCSme on Friday February 28, 2020 @02:38PM (#22445643)
I think my opinion is biased because I know React pretty well and have a lot of JS experience so the article feels just like explaining syntax and does not cover important edge cases (eg. what if you have localStorage disabled? your hook will crash!).

LE: I actually tested the site with Cookies disabled and the entire site crashed due to the localStorage calls.

Re:Persisting React State in LocalStorage by jbawn on Friday February 28, 2020 @12:25PM (#22444417)
Looks like it's called both ways:

useStickyState("count", 0);

Then later in the article:

useStickyState('day', 'calendar-view');

Re:Persisting React State in LocalStorage by joshwcomeau on Friday February 28, 2020 @01:08PM (#22444853)
Ah, good catch! Yeah, I changed this partway through, missed that!

Thanks for the heads-up.