I have the following layout:
<button onClick={joinSession}></button>
my Function
const joinSession = () => {
//set the value this will take some time
setState({...state, session : someLibrary.initSession()})
const session = state.session;
//it's undefined so the code never gets here...
session.on('event' , () => {
console.log('hello');
})
}
const [state, setState] = useState({
session: undefined,
})
The problem is that the function I call finishes running before the session state is set. I need some way to wait for the state to be set before running the rest of the code. Something like a .then would be nice. In class components you can use a second argument and pass it to setState, but i'm using functional components.
I tried using a promise :
new Promise(resolve => {
return setState({ ...state, session: OV.initSession() });
}).then(res => {
const session = ovState.session;
This didn't help..