I am trying to trigger a useEffect
in one of my components, when the state variable of another changes.
I have implemented web sockets into my app, and basically want to perform some action when the web socket is triggered. I have the web socket side of things covered and working right now.
This is a basic breakdown of what is going on:
HomeScreen
...
const {
shouldFetchApplications,
setShouldFetchApplications
} = useWebSockets();
...
useEffect(() => {
console.log(shouldFetchApplications)
// Other code
}, [shouldFetchApplications])
...
WebSocket
...
const {
setShouldFetchApplications,
} = useWebSockets()
...
const onTaskUpdate = () => {
setShouldFetchApplications(true)
};
...
useWebSockets
import { useState, useEffect } from 'react'
const useWebSockets = () => {
const [shouldFetchApplications, setShouldFetchApplications] = useState<boolean>(false)
useEffect(() => {
console.log('useWebSockets')
console.log(shouldFetchApplications)
}, [shouldFetchApplications, setShouldFetchApplications])
return {
shouldFetchApplications,
setShouldFetchApplications,
}
}
export default useWebSockets
When my web socket is triggered, it updates shouldFetchApplications
to be true, but the useEffect
in HomeScreen
never triggers, I know setState
is async, but I don't see why this would be an issue.
Any ideas on how I trigger the useEffect
in my HomeScreen
component?