Project feedback and handy utilities #2745
Replies: 3 comments 17 replies
-
This is awesome, thanks for sharing. How has you experience been migrating? |
Beta Was this translation helpful? Give feedback.
-
Before converting to loadable, it's a promise. However, useAtomValue const heavyPromiseAtom = atom((get) => ({ promise: get(heavyAtom) }));
function ExportButton() {
const { promise } = useAtomValue(heavyPromiseAtom);
async function onClick() {
// Heavy export operation
const data = await promise;
// Do something with data
}
return (<button onClick={onClick}>Export</button>);
} |
Beta Was this translation helpful? Give feedback.
-
Yeah, it explains.
Hm, I don't think it's possible. |
Beta Was this translation helpful? Give feedback.
-
Hey! I've recently migrated from Recoil to Jotai, and wanted to share some feedback as well as utilities I've made along the way to help me out with the process.
atomFamilyWithDefault
A really common pattern in my app was to asynchronously fetch an item from remote store, but also be able to write to it. While for
atom
we haveatomWithDefault
, foratomFamily
- you're pretty much on your own. So I came up with this:The type assertions are there to make it possible to write synchronous setters, like
setItem(prevItem => ({ ...prevItem, is_active: true }))
. Otherwise,prevItem
would need to be awaited, but in practice, you can't really (at least in my app) get a promise there. This problem also affectsatomFamily
, so I also have a type-asserting wrapper for it.createJointFamily
The requirement here was to have a joint family of atoms for:
/api/business/:id/items
)/api/business/:id/items/:itemId
)with the requirement of both
atomFamily
being writable and connected. Specifically:itemAtomFamily
if it was already fetched byitemsAtomFamily
itemsAtomFamily
should update the same items initemAtomFamily
itemAtomFamily
should update the same item initemAtomsFamily
Usage:
Source:
A simpler version, supporting only one param (for cases like a pair of APIs:
/api/businesses
and/api/businesses/:id
):Hope this helps anyone, whether it's the maintainers or the users :)
Beta Was this translation helpful? Give feedback.
All reactions