3 ms·
Okay this is very interesting, I love React but I'm also worried about the "complexity explosion" that is going on. As an example look at this "zombie children"
by franciscop 7y ago
Okay this is very interesting, I love React but I'm also worried about the "complexity explosion" that is going on. As an example look at this "zombie children" article that has bitten me when trying to build a library:
https://kaihao.dev/posts/Stale-props-and-zombie-children-in-Redux https://kaihao.dev/posts/Stale-props-and-zombie-children-in-...
Curious about Crank.js, would this timer be valid as well (it works on codesandbox)? How does it stop/unmount?
const delay = t => new Promise(ok => setTimeout(ok, t));
async function* Timer() {
let seconds = 0;
while (true) {
await delay(1000);
seconds++;
yield <div>Seconds: {seconds}</div>;
}
}
- bikeshaving 7y agoHi! Author here, thanks for the interest. Your example will just work! The only thing to worry about when using `while (true)` with async generator components, is that Crank will continuously pull values from the generator even if the renderer or a parent isn’t updating it, so if you forget to await something you’ll end up entering an infinite loop (which starves the microtask queue so it’s even worse than regular infinite loops). > How does it stop/unmount? Generators have this nifty feature where you can stop or `return` it from the outside. What this does is it resumes your generator at the most recent yield, but rather than continuing execution, it “returns” the generator at the point of execution. In other words, it’s almost like it turns your yield into a return statement. This is what breaks out of the `while (true)` and prevents your code from continuing to run when unmounted. The cool thing is that you can also then wrap the loop in a try/finally block, and execute some extra code when the generator is unmounted. If you have further questions let me know!
- franciscop 7y agoI didn't know you could forcefully end a generator from outside. IMHO generators are overkill 99% of the times. This is one of the biggest issues I had with the PREVIOUS Koa.js' documentation[1] and why I decided to launch Server.js with plain async instead of trying Koa. I had been doing JS for ~6 years by then and I could not tell how the simple "hello world" worked. [1] https://koajs.com/ https://koajs.com/ [2] https://serverjs.io/ https://serverjs.io/