react

"mulling over state management stuff"
orestis 2020-05-10T14:45:19.032200Z

I’m going from having fun with React experimental to tearing my hair out 🙂 I’m seeing things double: I’m adding subscriptions to an atom, and it seems that react is doing things twice but monkey-patching console.log 🙂 so the usual println debugging doesn’t work.

lilactown 2020-05-10T14:53:45.032700Z

hmm it's definitely not monkey patching console.log

lilactown 2020-05-10T14:54:54.033800Z

with the development bundle, React experimental will render your components twice as a sort of heuristic to check to make sure your components are concurrent mode safe

1👀
lilactown 2020-05-10T14:55:14.034200Z

if you paste some code / push a repro I can take a look

Aron 2020-05-10T15:12:37.034900Z

@orestis your components should work independent how many time render are called, this is true with class components and old react as well, no?

Aron 2020-05-10T15:13:52.035800Z

by default you get called twice afaik, once with nulls and once when it's completed, or any number of times between.

orestis 2020-05-10T15:16:35.036700Z

Oh I don’t mind getting called twice - but I expected the hooks to apply only once.

orestis 2020-05-10T15:19:31.040Z

So I use useEffect to subscribe to a store. I print the subscriptions to the store at the store level. The subscriptions are backed by an atom. I see 1 subscription printed, but after render is finished, my atom contains 2. Unless someone is monkeying with console log, I should see two prints too.

orestis 2020-05-10T15:20:10.041100Z

There’s also Suspense in all this, and I’m not sure how Suspense comes in the lifecycle. Or if there’s a bug in the build I’m using :)

orestis 2020-05-10T15:24:07.043200Z

I’m trying to figure out nested suspense boundaries and why my component stays suspended, and I ran into this weirdness above. I’ll keep noodling :)

Aron 2020-05-10T15:28:30.043600Z

could be thrown, swallowed

Aron 2020-05-10T15:29:15.044300Z

I would start small, and build up one by one, not try everything at once 🙂

Aron 2020-05-10T15:29:20.044500Z

but that's just me 😄

orestis 2020-05-10T15:41:28.044800Z

I did 🙂

orestis 2020-05-10T16:06:32.047500Z

The specific thing I’m trying to test is nested components suspending to a single outer suspense boundary. Something is weird at the first render. If shadow hot-reloads and calls render again at the concurrent root, everything clicks together.

orestis 2020-05-10T16:07:01.048500Z

I realize that without any code this is probably inscrutable to everyone except me :)

Aron 2020-05-10T16:08:39.048900Z

we all agree on that at least 😄

Aron 2020-05-10T16:10:15.049900Z

but if shadow, and if it's development mode, it has its hooks, I am sure you are using them, I mention them just to be doubly sure

lilactown 2020-05-10T17:38:24.050300Z

here's what I've been experimenting with the last couple weeks: https://github.com/Lokeh/pine

orestis 2020-05-10T17:56:02.050700Z

https://github.com/orestis/reseda

orestis 2020-05-10T17:56:54.051900Z

This is my attempt. The first demo is a copy of reagent’s demo, BMI calculator. The second attempt fetches some remote stuff, and is trying to use Suspense. I can see the promises resolving, but my components don’t resume. Any pointers welcome.

orestis 2020-05-10T18:13:29.053Z

Of course, now I found the issue — you shouldn’t provide React a never-resuming Suspense. However it’s counter-intuitive to me, unless something was raised and caught inside React which I never saw.

orestis 2020-05-11T07:11:20.055400Z

Some possibly-related issues that might explain what’s going on: https://github.com/facebook/react/issues/16954 https://github.com/facebook/react/issues/18749

1👍
orestis 2020-05-11T08:16:04.055900Z

Managed to reproduce with plain JS: https://github.com/facebook/react/issues/18885

1👌
Aron 2020-05-11T09:03:50.056200Z

just a nitpicking question, but why use let ? It signals that the value should be changed, should be used sparingly imho, like in for-loops and similar

Aron 2020-05-11T09:06:20.056400Z

I had similar issue using stuff like redux devtools where the subscription came earlier than the tool was booted so it missed it consistently : )]

orestis 2020-05-11T12:05:30.056600Z

I haven’t written JS for a long time 🙂

1👍
orestis 2020-05-10T18:18:23.054300Z

I’ll be pushing things to reseda over the coming weeks. Interested to hear feedback about the general approach. Once I’m reasonably confident that it can be used to build a real app I will probably make a more general announcement.

orestis 2020-05-10T18:27:13.054400Z

It only seems to be an issue in the initial render. If I do it manually (at the REPL) it recovers when provided a correct value. I’ll be making a sandbox to see if it’s a React issue.