0

I have researched on immediate state update using hooks and found this solution. However, I am finding a challenge implementing it in my case.

const rowEvents = {
  onClick: (e, row, rowIndex) => {
    setMessageid(row.pm_id);      
    console.log(row.pm_id);
    console.log(messageid);
    if (row.custompassword === true) {
      handleShow1();
    } else {
      decipher();
      handleShow2();
    }
  },
};

How do I get the current set state of messageid by adding it to useEffect so that it detects any changes? decipher() uses messageid therefore it needs to be current one.

Denn
  • 447
  • 1
  • 6
  • 27

1 Answers1

1

If i understand your logic correctly you can have the decipher() function, inside a useEffect and just set the state like usual. Just remember to set the message id as a dependency.


const [messageId, setMessageId] = useState("");

useEffect(() =>{
  decipher()
  //useEffect will be called after the messsageId state changes
}, [messageId])

const rowEvents = {
   onClick: (e, row, rowindex) =>{
   // do other stuff
   setMessageId(row.pm_id)
   }
}

Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69