D
D
Durin Qurkingo2019-08-03 12:07:53
React
Durin Qurkingo, 2019-08-03 12:07:53

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

1 answer(s)
A
Andrey Kostyaev, 2019-08-03
@Sagund

const usePrevious = value => {
  const ref = useRef()
  useEffect(() => {
    ref.current = value
  }, [value])
  return ref.current
}


const [isOpen, setOpen] = useState(false)
const previous = usePrevious(isOpen)

I don't know if I understood you correctly, but maybe this will work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question