Skip to content

Instantly share code, notes, and snippets.

@coodoo
Last active January 31, 2023 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coodoo/c0b726775955ce1b622fa70dd2d2b0f2 to your computer and use it in GitHub Desktop.
Save coodoo/c0b726775955ce1b622fa70dd2d2b0f2 to your computer and use it in GitHub Desktop.
// sample code copied from the blog:
// https://blog.axlight.com/posts/you-might-not-need-react-query-for-jotai/
const idAtom = atom(1)
const dataAtom = atom(
// read
async (get) => {
const id = get(idAtom)
const res = await fetch(`https://reqres.in/api/posts/${id}`)
const data = await res.json()
return data
},
// write
async (get, write, payload) => {
store.set(idAtom, payload)
}
)
// my question here
// in react
const Comp = props => {
// Is it recommended to invoke both getter and setter provided by dataAtom?
const [post, setPost] = useAtom(dataAtom)
// so that when user selected another post, we invoke setter on `dataAtom` instead of `idAtom`
// and let dataAtom set the origin idAtom for us?
// or is there better ways to do this?
const handle_post_change = post_id => setPost(post_id)
//
return `render something`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment