I have a problem with React and Redux and I imagine finding a solution here ... On my application, I have a function when loading the page componentDidMount which calls a function that lists images on my page. I also have a button that calls the same function.
My problem: when I click on the button, although I already have the list of displayed images, I have the same list twice and if I click again, the same new images are added. It's only when I reload the page that everything comes back to the way it started, I searched in vain, Redux keeps the data and adds the same data each time we call it unless we reload the page.
case FETCH_GALLERY_SUCCESS:
return {
...state,
loading: false,
// stories: state.stories.concat(action.payload),
stories: [...state.stories, ...action.payload],
currentPage: action.currentPage,
totalPages: action.totalPages,
totalStories: action.totalStories,
error: ""
}
Finilly I found this :
case FETCH_GALLERY_SUCCESS:
return {
...state,
loading: false,
stories: [...state.stories, ...action.payload].filter(
(storie, index) => index === [...state.stories, ...action.payload].findIndex(
elem => elem.id === storie.id && elem.id === storie.id
)
),
currentPage: action.currentPage,
totalPages: action.totalPages,
totalStories: action.totalStories,
error: ""
}