Closed
Description
I just recently went through a refactoring from recoil. Everything was pretty straightforward but one use case I couldn't build in jotai.
I have a list of countries I can fetch from the backend:
const availableCountriesApi = atom<Array<Country>>(async () => fetchEntities("/core/countries"))
I built a defaultCountry atom based on this fetch
const defaultCountry = atom<Country>(
(get) => get(availableCountries).find(country => country.id === "US") || {id: "US", name: "United States"},
)
And now I have a selectedCountry
atom which I want to initialize with the default country. But everything I tried resulted either in a type error or a readable only atom:
const selectedCountryState = atom<Country>((get) => get(defaultCountry))
const [selectedCountry, setSelectedCountry] = useAtom(selectedCountryState)
setSelectedCountry({id: "DE", name: "Germany"}) // This expression is not callable, Type 'never' has no call signatures.
I think the async fetch is not super relevant, because when I change my defaultCoutry atom to this, I'll run in the same problem:
const defaultCountry = atom<Country>({id: "US", name: "United States"})
So is there a way to build a writeable atom that I can initialize with a derived atom value?
Metadata
Metadata
Assignees
Labels
No labels