Last active
January 31, 2023 06:48
-
-
Save coodoo/c0b726775955ce1b622fa70dd2d2b0f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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