Weird issue in State of react-native

Viewed 35

I have redux implemented in my react-native app and its working, I get value from the store and log it and it will be there. The problem is when I press a button and call a function then log inside the function for that field, it will be empty

Here is what I mean:

const [data, setData] = useState(trolly || {}); // trolly from redux store

useEffect(() => {
  if (trolly) {
    console.log('SETTING-DATA::', trolly);
    setData(trolly);
  } else {
    getTrolly(trollyId);
  } 
}, [trolly]);



const handleSelect = (id) => {
  console.log('DATA:: ', data);
}

Also, this will work when I refresh the page again

When I log I get the SETTING-DATA with the value I want, but when I trigger handleSelect data will be empty.

How is that possible, What I'm doing wrong here

1 Answers

You dont need to use local state in your case

import {  useSelector } from 'react-redux';
        const data = useSelector((state) => state.trolly);
        
        useEffect(() => {
         getTrolly(trollyId);
        }, [trolly]);
        
        const handleSelect = (id) => {
          console.log('DATA:: ', data);
        }
Related