5 ms·
Once you add useMemo and useCallback, your code will become pretty unsightly though. I wish React had a better solution to this problem.
by continuational 4y ago
Once you add useMemo and useCallback, your code will become pretty unsightly though. I wish React had a better solution to this problem.
- gedy 4y agoAgreed, many libraries and frameworks have built in support and simpler syntax for computed values, like MobX, or even older frameworks Ractive or Knockout. React takes a very "code-y" approach to this, which I guess some people like.
- HeyImAlex 4y agoI’m in the process of converting a large knockout app to react and computed values are definitely what I miss most. They’re defined on the class (normal react practices prefer everything to be a plain object), they can reference other values/computeds internally just by calling this.computed (in react you have to manually chain the results through useMemo calls, and even with reselect there’s the added step of specifying all of your dependencies), they’re computed on demand and track their dependencies lazily (in react if you want a conditional useMemo you have to just early return null from the callback), and they’re conveniently useable in both model code and component code. I haven’t found a way to convert these that doesn’t feel like a complete regression in terms of code readability and ease of use.
- brundolf 4y agoThis talk suggests they're exploring ways to improve the DX: https://youtu.be/lGEMwh32soc https://youtu.be/lGEMwh32soc
- harrisonjackson 4y agoYou can largely avoid useMemo by keeping your props and state in the format you actually need them. The solution for more complex useMemo and useCallback usage is to create your own hooks. You can compose all the hooks into a single hook that your component uses and return from that hook whatever your component needs. This is common with components that have a lot of event listeners that can trigger local state and/or redux state changes. We have a spreadsheet component and the individual cells had some pretty gnarly hooks and after pulling them into their own custom hook we've been able to maintain them nicely.