I have a React Native Expo application with Jotai, but the Jotai atom is always null.
App.tsx
const App = () => {
const [organisation, setOrganisation] = useAtom(organisationAtom)
useAsyncEffect(async () => {
const value = await read<User>("user")
setOrganisation(value)
}, [])
return (
<NavigationContainer>
{organisation ? <BottomTabNavigator/> : <StepNavigator/>}
</NavigationContainer>
)
};
export default App;
HomeScreen.tsx:
export const HomeScreen = () => {
const [organisation, setOrganisation] = useAtom(organisationAtom)
const handle = async ({data}: BarCodeEvent) => {
const json: AttachResponse = JSON.parse(data);
if (json.organisation == undefined || json.token == undefined) {
alert(i18n.t('alerts.scan.invalid'));
return;
}
await write("user", json)
setOrganisation(json)
console.log("StepThree json " + json.token)
console.log("StepThree org " + organisation!!.token)
}
return <Scanner onScanned={(data) => handle(data)}/>
}
state.ts
export const organisationAtom = atom<User|null>(null)
I've tried many things, but unfortunately I'm unable to make it behave differently.
PS: The two logs log this AFTER a new startup (when the secure store is already filled):
StepThree json o4k7sdBG0T4H3uqvWFFSMPJMZxealYUGdUe
StepThree org o4k7sdBG0T4H3uqvWFFSMPJMZxealYUGdUe
And this when the application is fresh (no secure store values):
StepThree json o4k7sdBG0T4H3uqvWFFSMPJMZxealYUGdUe
Organisation is null (TypeError: null is not an object (evaluating 'organisation.token'))
So it seems that Jotai is reactive after a restart?