J
J
JordanBelford2021-05-12 08:16:25
React
JordanBelford, 2021-05-12 08:16:25

Why is the redux store full of duplicates?

Good afternoon! I was faced with the task of implementing pagination, for this the following steps were taken:
The first step was to create a new function in the reducer:

case SET_CURRENT_PAGE: {
    return {
        ...state,
        currentPage: action.payload
    }
}

The second step was to create a network request:
useEffect(() => {
        axios.get(`/post/${props.numbersPage.currentPage}`)
            .then(res => {
                res.data.forEach(item => {
                    props.addPost(item.id);
            });
    }, [props.numbersPage.currentPage]);

In order for useEffect to work after changing the current page, the dependency specified currentPage.
The last step is adding page rendering:
<div className="page">
    {pages.map((page, index) => <span 
        onClick={() => dispatch(setCurrentPageAC(page))} 
        key={index}>{page}
    </span>)}
</div>

Everything works fine, except for one thing, but why, when I click on the span, the old state of the redux store is not overwritten by the new one, the old records remain, and a new response from the server is added to the end. This is not the first time I encounter this problem, I can assume that I may be copying in the reducer incorrectly. Who knows how to solve this problem?

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