I'm trying to create a react component that calls a callback function only when a prop changes from false to true. I can't have the parent control the callback directly because I need to pass child state into the callback. I understand I could pull the child state up to the parent, but in trying that, that made my code really messy, so I would like to avoid that if at all possible. In other words:
// not proper typescript but I don't care const MyComponent = ({shouldUpdateState: boolean, updateParentState: Dispatch<CustomState>}): FC => { const [someVariableThatExistsInsideThisComponent, useSomeVariableThatExistsInsideThisComponent] = useState<CustomState>(DEFAULT_CUSTOM_STATE) // how do I write this line? if(shouldUpdateState && reasonForRerender === changeTo(shouldUpdateState)) { updateParentState(someVariableThatExistsInsideThisComponent) } ... }