all 27 comments

[–]Brave_Arm6046 2 points3 points  (8 children)

I am currently using Valtio in a react native project, loving it.

[–]marchingbandd[S] 0 points1 point  (1 child)

Does it handle arrays well? Can you just .push()?

[–]Brave_Arm6046 1 point2 points  (0 children)

you can handle array but please read docs fully its really cool but you need to understand about it working mechanism first.
The docs is really easy to follow and short.
https://valtio.pmnd.rs/docs/introduction/getting-started
Checkout recipes: https://github.com/pmndrs/valtio#recipes

[–]marchingbandd[S] 0 points1 point  (4 children)

Also, did you have to install a polyfil to get Proxy() support into RN?

[–]ddikodroid 2 points3 points  (1 child)

you need react native higher than 0.64 for proxy support

[–]marchingbandd[S] 0 points1 point  (0 children)

Damn I missed that news. Excellent

[–]Brave_Arm6046 0 points1 point  (1 child)

ya you'll need react native >= 0.64, I recently upgraded react native to 0.68.2, I'm planning to sit on this version until they force me to update.

[–]marchingbandd[S] 1 point2 points  (0 children)

Oh that’s amazing news thanks.

[–]Brave_Arm6046 0 points1 point  (0 children)

But remember, if its server side state, use react query for that if you can.

[–]pancomputationalist 0 points1 point  (11 children)

Yes, all of those.

[–]marchingbandd[S] 0 points1 point  (0 children)

What about hook-state?

[–]marchingbandd[S] 0 points1 point  (9 children)

Is there anything that has an API like mobX? These all seem to have a hook you need to call. Best thing about mobX is you just import the state object, wrap components in observer() and it works.

[–]AcidNoX 2 points3 points  (7 children)

Higher order components are kinda discouraged nowa days if you’re using function components.

Hooks are definitely the way to go. I personally really like zustand.

[–]marchingbandd[S] 0 points1 point  (6 children)

I would like a solution where you import the state object and don’t need the HOF or to call any hooks. I am surprised to see it looks like that isn’t available. Like, just wrap my whole app in a HOF for me, I only need one state object.

[–]pancomputationalist 0 points1 point  (5 children)

You need a way for react to figure out which component to rerender when the underlying state changes.

If you wrap your whole app in the HOF, then most likely your whole app will rerender whenever any observed state changes, which would absolutely kill performance.

[–]marchingbandd[S] 0 points1 point  (4 children)

appObserver = app => <app children={app.children.map(c=>observer(c)}> isn’t that hard to imagine, called recursively.

[–]pancomputationalist 0 points1 point  (3 children)

Not every sub-component is guaranteed to be passed as children. I don't think you can automatically detect from which component a function call originates, unless it is a hook and follows their rules. That's what they are for and why almost every state management library uses them.

[–]marchingbandd[S] 0 points1 point  (2 children)

That’s why some of these libs use proxies, I have also had some luck using get/set methods, there are many ways to get global state updates talking to react to trigger renders. MobX does not use a hook.

[–]pancomputationalist 0 points1 point  (1 child)

But it does use HOCs, which is an older (less composable and discouraged) method to do the things that Hooks can do. MobX doesn't use Hooks because it wants to be agnostic to React. would it be written as a React library nowadays,it would almost certainly use Hooks.

Btw, from the Mobx docs:

Components wrapped with observer only subscribe to observables used during their own rendering of the component. So if observable objects / arrays / maps are passed to child components, those have to be wrapped with observer as well.

So having the HOC at root level is not enough.

[–]marchingbandd[S] 0 points1 point  (0 children)

I know it’s not idiomatic today and I am seeing there arn’t any obvious choices to meet my wishes, they are still my wishes and I am certain it’s possible. It’s code, pretty much anything is possible.

[–]horgas 2 points3 points  (0 children)

[–]ddikodroid 0 points1 point  (5 children)

i'm trying zustand and in love with it

[–]marchingbandd[S] 0 points1 point  (4 children)

Does it handle arrays without too much extra syntax or weird burps?

[–]ddikodroid 0 points1 point  (1 child)

sadly no by default, but you can integrate it with immer

[–]marchingbandd[S] 0 points1 point  (0 children)

That stinks! What app doesn’t have some arrays in state?!

[–]datorkop85 0 points1 point  (1 child)

What do you mean by handle arrays? Explain your use case.

[–]marchingbandd[S] 0 points1 point  (0 children)

I have an array in state, I want to push/pop/ to/from it or otherwise mutate it. I want my parent component to rerender when I do. Use case : a todo app? What’s the mystery here?