6 ms·
>React doesn't provide a systematic answer for handling state in apps if data is flowing up, down and sideways So let's first back up and recognize that this e
by stillkicking 4y ago
>React doesn't provide a systematic answer for handling state in apps if data is flowing up, down and sideways
So let's first back up and recognize that this earlier statement was flat out wrong. React does provide a systematic answer for this.
Second, not only does it have a systematic answer, but it memoizes quite well because React will not re-render children if the `children` prop is identical to the previous render, even if you don't use `memo()`. This means it is quite cheap to have context providers update, even if you nest 2 or 3 of them.
The big issue with React in my experience is just that developers are lazy af and will stubbornly refuse to read even the tersest of docs even if they are encountering a new paradigm, like declarative and reactive UI. The result is a giant spaghetti mess of their own creation, which they then blame the framework for.
You can make React fast and you can keep it clean, all you have to do is topologically sort your data by the frequency of how quickly it changes. That's it. That's the trick.
- vbezhenar 4y agoMy issue with React is that it's truly hard. It markets itself as easy but it's not. I have 20 years of programming experience, I dealt with UI a lot, I used WinAPI, Java Swing, I know JS and HTML pretty well. I'm fine with reactive programming or async stuff. Yet I often struggle with React. I'm not a full-time web developer, I admit, I'm more like full-stack developer but when I need to write novel React code, I struggle a lot. For example recently I wanted to use a promises in React app. I mean: promises are as native to JS as it gets. Surely React should have first-class support for promises. Nope. So I started to write custom hook. usePromise. Like useEffect, but for promises. Well, it would not be hard. But apparently React likes to call useEffect twice for dev mode. So I need to have a reliable cancellation. How do we cancel stuff in web? With AbortController, right. Does React heard about AbortController? Nope. So I need to integrate AbortSignal within my usePromise hook. I read famous Dan Abramov article, I read other articles, I spent days tinkering around this thing, I wrote several implementations. All of those implementations are faulty. Here's my latest one: https://pastebin.com/WBctCBpc https://pastebin.com/WBctCBpc. Technically it works. But it contains unpure reducer function. It's not broken in current React version. But who knows how react dev mode will torture my reducer in the next version. I have to admit that I enjoyed toying with this stuff. But it definitely counter-productive to business values. Now I know that this is all solved and I should just use react-query or something similar. Well, I have my reasons to avoid it. But my point still holds: React is hard, React is not well integrated with JS and Web. And probably React will get better in the future. I've heard about suspension stuff which might just be what I need, but it's not there yet.
- scotty79 4y agoWhat does usePromise is supposed to do?
- vbezhenar 4y agoIt's supposed to run provided promise and return its status. If deps changed or component is unmounted while promise is pending, it should inform currently running promise using AbortSignal. And it should handle edge cases (e.g. promise is changed, second promise is started but first promise ignored abort signal and resolved to a value. This value should be ignored). Basically it should remove any boilerplate from user of this API and handle edge cases.
- scotty79 4y agoThanks. But what do you use it for? What promises do you want your components to be involved with and in what way?
- vbezhenar 4y agoFor example HTTP request. Anything, actually. Some rough code: function Item({id}) { const r = usePromise(async (signal) => { const resp = await fetch(`/item/${id}`, {signal}); return await resp.json(); }, [id]); if (r.status == "pending") { return <div>Loading</div>; } if (r.status == "rejected") { return <div>Error: {r.reason}</div>; } return <div>{r.value}</div>; } It's really like useEffect but provides better support for cancellation and properly tracks promise. Rewriting this snippet with useEffect correctly would require quite a lot of code (although rewriting this snippet with useEffect incorrectly is possible with not a lot of code, but you don't want to write incorrect code). Which has to be repeated everywhere. Again, this task is better solved by react-query or its alternatives. What I'm writing is not strictly web-site, but rather a web-interface on embedded device and web-server is not remote web-server but thing that works on the same device, so for now I decided not to use those libraries which made for slightly different use-cases.
- PaulHoule 4y agoNo, the troubles building an SPA have a lot to do with the complexity of your app. If you are building the average mobile app it is often really clear when you are writing code what needs to be updated in the UI when a piece comes in. If you are building something more like Adobe Photoshop or Eclipse the user has the ability to open up property sheets and create other UI elements that could be affected by data structures anywhere in the application. In that case you need some systematic answer such as components registering to get notifications when something happens but you can run into some pretty bad problems such as having to hang on to references which keep the garbage collector from working as expected. My first SPA was a knowledge graph editor in GWT that I managed to get correct (though it probably leaked memory a little) and since then I haven't known whether to laugh or cry about the state of SPA frameworks. As for the manuals I think the React manuals are some of the worst in the business. I have no problems finding answers in the Java manual or the Python manual or the Postgres manual or many others but the React manual baffles me.
- hbrn 4y ago> So let's first back up and recognize that this earlier statement was flat out wrong. React does provide a systematic answer for this. Context was never a systematic answer. Even today the docs say: Apply it sparingly because it makes component reuse more difficult. If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context. https://reactjs.org/docs/context.html#before-you-use-context https://reactjs.org/docs/context.html#before-you-use-context And IIRC older docs would be even more harsh at recommending not to use context.