React Native app's expo-sqlite callback not executed until app refresh
When the app starts, the initial db
value is set. The first effect batches setDb
and fetchDicts
, so db.transaction
has not been updated with the value of the setDb
call. Because Expo has stateful refresh, the db
is no longer set as the initial value but has the result of setDb
, so fetchDicts
works, and the results appear.
One way to fix this would be to move fetchDicts
into its own effect, and call it when db
changes.
useEffect(() => {
openDatabase().then((value) => {
setDb(value);
});
}, []);
useEffect(() => {
fetchDicts(); // will do nothing until db connection is open
}, [db]);