Replies: 9 comments 8 replies
-
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. |
Beta Was this translation helpful? Give feedback.
-
++ 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! |
Beta Was this translation helpful? Give feedback.
-
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" |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
Even if atom configs are defined at module level, they don't hold values. It's |
Beta Was this translation helpful? Give feedback.
-
@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 ? |
Beta Was this translation helpful? Give feedback.
-
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.) |
Beta Was this translation helpful? Give feedback.
-
I didn't get a good idea about dealing with other hooks from third-party libraries, such as react-navigation. |
Beta Was this translation helpful? Give feedback.
-
Hey is there a "TL;DR" summary, of the best practices? Or any consensus? |
Beta Was this translation helpful? Give feedback.
-
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?
Beta Was this translation helpful? Give feedback.
All reactions