I have the following code, how do I prevent AsyncStorage.setItem from being called 2 times on initial render? It's called with [] and whatever is loaded from AsyncStorage because logs was updated. The perfect solution should not call setItem at all, because the logs was just retrieved from AsyncStorage.
const AsyncStorage = require("@react-native-community/async-storage") const useStore = () => { const [logs, setLogs] = useState([]) useEffect(() => { AsyncStorage.getItem("logs").then((newLogs) => setLogs(newLogs)); }, []) useEffect(() => { //Don't want to setItem on initial load or when `logs` was just loaded. AsyncStorage.setItem("logs", JSON.stringify(logs)); }, [logs]) const addLog = (newText) => { setLogs(logs => [ {text: newText, createdAt: new Date().getTime()}, ...logs, ]); } return { logs, addLog, //...many other functions that update logs } }
componentDidUpdatehook?