There are parts of my app that must work synchronously. I am using zustand. The problem is that zustand's setState function works asynchronously. Please let me know if there are any other libraries that support synchronous state changes or any tricks.
App.js (react example)
// in real
import create from 'zustand'
import logo from './logo.svg';
import React, { useEffect } from 'react';
const useStore = create(() => ({
counter: 0
}))
function App() {
const { counter } = useStore()
useEffect(() => {
console.log(counter) // output: 0
useStore.setState({ counter: counter + 1 })
console.log(counter) // output: 0
useStore.setState({ counter: counter + 1 })
console.log(counter) // output: 0
}, []);
return (
<div></div>
);
}
export default App;
// my hope
// ...
useEffect(() => {
console.log(counter) // output: 0
useStore.setState({ counter: counter + 1 })
console.log(counter) // output: 1
useStore.setState({ counter: counter + 1 })
console.log(counter) // output: 2
}, []);
// ...