Answer the question
In order to leave comments, you need to log in
How to check if default state boolean changes from false to true with React Hook useEffect?
I'm currently completely stuck with React Hook .
When the state changes true it should show console.log('currentBoolean') but it doesn't.
Not sure if I should use usePrevious because when comparing numbers it works well with this code, but it doesn't seem to work when I try to check if the default boolean changes.
const MemoryPageTwo = ({ disabledAllCards }) => {
const boolean = useRef(null);
const [ isCurrentBoolean , setLevel ] = useState(false);
useEffect(() => {
console.log(isCurrentBoolean);
if(isCurrentBoolean) {
console.log('currentBoolean');
}
}, [isCurrentBoolean]);
useEffect(() => {
const storedBoolean = disabledAllCards ;
boolean.current = storedBoolean;
return () => storedBoolean
}, []);
//
useEffect(() => {
setLevel(boolean.current !== disabledAllCards);
},[boolean.current, disabledAllCards]);
}
const mapStateToProps = state => {
console.log(state.app.disableCards); // default is false, but this will be true if the memorycard matches
return {
disabledAllCards: state.app.disableCards || [],
}
}
export default connect(mapStateToProps, null)(MemoryPageTwo);
Answer the question
In order to leave comments, you need to log in
const usePrevious = value => {
const ref = useRef()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}
const [isOpen, setOpen] = useState(false)
const previous = usePrevious(isOpen)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question