How does it work with jotai?
Closed this issue · 4 comments
Is it possible to somehow bridge jotai's atom to use-travel?
Currently, use-travel
does not support this; it's a special jotai atom. In fact, Immer behaves the same way when used within jotai.
For example,
import { useAtom } from 'jotai'
import { atomWithMutative } from 'jotai-mutative'
const countAtom = atomWithMutative({ value: 0 })
const Counter = () => {
const [count] = useAtom(countAtom)
return <div>count: {count.value}</div>
}
However, I'm curious why use-travel
needs support for jotai. It should be an independent React hook that can be used upon state initialization; it doesn't require jotai atoms.
There is currently no time travel implementation in the jotai ecosystem.
I found this current project and I looked at the source code, and I thought for a moment that it might be possible to support an initial value as a jotai atom.
I'd like to clarify that regardless of which state library you use, in fact, this hook only supports a single state, so it doesn't require state atomization.
If you have an external jotai atom, then when initializing the component's state, you can convert it into a internal state and use it as the initial state for use-travel
.
You can use useEffect
to listen for its changes, and when this state changes, you can sync it to jotai.
use-travel
is just a React hook; perhaps what you really need is a jotai-travel
lib.
hi @StringKe, You might want to check this out. https://codesandbox.io/embed/jotai-tutorial-09-lhz3e