H
H
hood522020-03-17 11:14:12
React
hood52, 2020-03-17 11:14:12

Delays in react-redux, is this normal?

Good, a question arose - in a react application, by clicking on a block, a function is called

const handleClick = (e) => {
    dispatch(clickActions.add([e.clientX, e.clientY]));
}

in the functional component itself, the data is received through the clickAction action and the corresponding reducer is as primitive as possible - one returns 'ADD' and the array itself that was passed to it, and the second adds data to the store. The question is the following. right after the dispatch, I decided to use the points constant - after all, it is logical that I made a dispatch and it would be time to use the data. somewhere inside I understand that something is wrong here, can someone explain in detail and in detail where I turned the wrong way in my thoughts? UPD1: if you need to use the data right now and immediately, maybe write it to a variable, and then synchronize it with the global store? Or is it going to be so dirty? UPD2: component code:
const points = useSelector(state => state.points)


// ... импорты

const Comp = () => {
    const dispatch = useDispatch();
    const points = useSelector(state => state.points);

    const handleClick = (e) => {
        dispatch(actions.clickActions.add([e.clientX, e.clientY]);

        // вот тут я уже хочу использовать данные из стора
       console.log(points); // Выводится старые данные, до диспатча, вопрос как все это дело переделать
    }

    return <button onClick={handleClick}>Click</button>;
}

// ... экспорт


UPD3: ok, it seems I just didn't fully understand the component lifecycle

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question