-
-
Notifications
You must be signed in to change notification settings - Fork 617
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
best practise using jotai in big project? #683
Comments
Yeah, I noticed that too. Now, I would write all logic in atoms and I wouldn't use custom hooks. (Especially, that would allow me to run the app in my experimental jotai-jsx.)
It's still an open question. I think both are valid. I'd prefer splitting atom files and only exporting atoms that are necessary.
I think we are still at the stage of building best practices. Help wanted. |
++ to putting as much mutation logic into writable atoms! it gives a nice separation similar to MVC where jotai atoms handle Model and Controller, and React FC handles View. these are some incomplete thoughts, but some best practices i've observed in my own work (two spatial tools: https://sprout.place & https://felt.com)
some other best practices im still figuring out, curious to hear other peoples patterns! |
on the question of when to use atom/when not to, this is something i've discussed often with my friends — in particular, one pattern i see a lot is how to capture some state from one component and pass it to a newly created component this specifically comes up in drawing tools type interaction — select a Text Tool, then create a new Text Element. This new Text Element should autofocus — but how does the newly created Text Element know if it's actually just created, or if it's only being remounted (i.e. on refresh) one way would be to use an atom, i.e. and in TextElement, we just look at if the ID matches
however, it might feel overkill, because it doesn't REALLY need to be reactive, as it'll only ever happen once to an element. the alternative here would be to make this and then we just check it on mount to focus the element imperatively, or we could use a shouldFocus useState locally
this is def one of those situations where I'm like "is an atom appropriate?" and it always comes back to "does it need to be reactive? or can it simply be checked imperatively" |
I´m wondering how to model the lifecycle of atoms. Using React contexts all contained state (e.g. in useState hooks) is removed as soon as we stop rendering the context provider. This comes in handy if you have an app with two pages where one page e.g. needs a websocket connection to the backend (and some additional state) which can be wrapped up in a Atoms on the other hand look like global entites which are never GC´ed. Do I miss something? |
Even if atom configs are defined at module level, they don't hold values. It's |
@dai-shi Thank you for the tip. I wasn´t aware of the tight coupling with React Context. This makes sense especially for bigger projects. Does Jotai require a context bridge for react-three-fiber ? |
If we use a default store (called Provider-less mode), then we don't need the context bridge (and we don't get much benefit from Context either). If we use a Provider, it doesn't work with r3f. (we once provided a context bridge function, but removed before v1.) |
I didn't get a good idea about dealing with other hooks from third-party libraries, such as react-navigation. |
@dai-shi maybe it's better to transform this issue to discuss. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Now I just treat jotai as state management tool, but I think jotai can do more than that.
I read the docs, and noticed that maybe I can write my logic in the derived atom and I don't need to put them in custom hooks anymore.
simply like this:
but then my question is, how should I organize the atom structure? should I put theme together or split them into serval files?
when should I use atom, and when shouldn't I use it?
all the examples in the doc is too simple. Is there any best practices to demonstate the best way of using jotai?
The text was updated successfully, but these errors were encountered: