0

Essentially I have this state object and this method:

const [groupData, setGroupData] = useState({});
// groupData state 
        groupData = {
           group1: [
              { id: 1, name: Mike, age: 24 },
              { id: 2, name: Bob, age: 31 }
           ],
           group2: [
              { id: 3, name: Christin, age: 21 },
              { id: 4, name: Michelle, age: 33 }
           ],
        }

const stateRef = useRef();

stateRef.current = groupData;

const handleRemovePerson = (personToRemoveById: string) => {
   
    const filteredGroupData = Object.fromEntries(
      Object.entries(groupData).map(([key, value]) => {
        return [key, value.filter((person) => person.id !== personToRemoveById)];
      }),
    );

    setMainContentData(filteredGroupData);

    // Now check if group array does not have anymore persons, if empty then delete 
       group array

    console.log('stateRef', stateRef);
    // outputs the correct current data
    const filteredStateRef = Object.keys(stateRef).map((key) => stateRef[key]);

    console.log('filteredStateRef', filteredStateRef);
    // outputs previous data ??
};

I tried useRef and once I loop through it, it gives me back the previous data. How can I get the most current data after setting state and then operating on that new data right away? Thank you!

6
  • what is your react version? Commented Feb 3, 2022 at 20:08
  • what does the state which you set at setMainContentData(filteredGroupData); used and where is it defined?
    – Ben
    Commented Feb 3, 2022 at 20:10
  • Might be an issue with the logging of the dev tools. Try console.log('stateRef', JSON.stringify(stateRef)); and console.log('filteredStateRef', JSON.stringify(filteredStateRef)); Commented Feb 3, 2022 at 20:11
  • @TaghiKhavari 17.0.2 Commented Feb 4, 2022 at 0:46
  • @Ben it is used to pass to a component which renders the data in the dashboard. Commented Feb 4, 2022 at 0:47

1 Answer 1

2

First of all, you can't access the state after using setState because it's an asynchronous operation and if you want to check something after using setState you need use useEffect hook to listen for state change and decide what to do with it, or in recent versions of react and react-dom you could use a not so suggested function called flushSync which will would update the state synchronously.

so the prefered way is to use useEffect like this:

const handleRemovePerson = (personToRemoveById: string) => {
  const filteredGroupData = Object.fromEntries(
    Object.entries(groupData).map(([key, value]) => {
      return [key, value.filter((person) => person.id !== personToRemoveById)];
    }),
  );

  setMainContentData(filteredGroupData);
};

useEffect(() => {
  if(true/**some conditions to prevents extra stuff */){
    // do your things, for example:
    console.log('stateRef', stateRef);
    // outputs the correct current data
    const filteredStateRef = Object.keys(stateRef).map((key) => stateRef[key]);
  }
}, [mainContentData, /**or other dependencies based on your needs */])
2
  • Thank you for this answer! A follow up question is how can I check that if one of the group[index] array is empty then return a new state with out that empty group? Commented Feb 3, 2022 at 21:02
  • @JohnMcGellan edit your question and paste the whole component and a sample of the data you want to work with, so I could see the whole picture and can help you better Commented Feb 4, 2022 at 5:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.