Search code examples
reactjsreact-redux

Redux useSelector don't work when called by a publish pub/sub


I have an issue with my eventBus and data from useSelector Below my sample


const toto = useSelector(state => state.toto);

const test = () => {
   console.log(toto); // always null
}

useEffect(()=>{
    console.log(toto); // always good when toto change whatever in my components
}, [toto]);

useEffect(()=>{
   subscribe('showtoto', test); 
   subscribe('showtoto', () => test()); 
},[])

i update toto in some components, the useEffect show always the mutation and the good data for toto. After in a component i publish the event 'showtoto'. test is executed but toto is always null.

Anyone have an idea, what's wrong? Thanks for your help.


Solution

  • add the todo property to dependency array in useEffect

    useEffect(()=>{
       test()
       subscribe('showtoto', test); 
       subscribe('showtoto', () => test()); 
    },[todo])