I have two components.
function Parent(props){
const handleClick = () => {
console.log(props.stateA);
};
return <div><Child text={stateB} handleClick={handleClick} /></div>
}
const mapStateToProps = (state) => {
return {
stateA: state.stateA // stateA will be changed somewhere else
stateB: state.stateB
}
};
export default connect(mapStateToProps)(Parent);
function Child(props) {
return <div onClick={props.handleClick}>{props.text}</div>
}
export default React.memo(Child,(prev, next) => {
return prev.text === next.text
});
My problem is when stateA is changed somewhere, clicking on Child
will log the previous stateA. I can't access the latest stateA.
You can see, I don't want to Child
re-render when stateA changes,it should re-render only when stateB changed. But I want to access the latest stateA in Parent
when clicking on Child
.
Is there any method to solve this problem?
If the
Parent
component is a functional component then you can use like thisI hope it'll work for you.