Answer the question
In order to leave comments, you need to log in
How to update only one element of an array in Redux?
Hello!
I have a state in redux - an array of elements. Using a hook useSelector()
, I get this array from redux
const places = useSelector(state => state.project.places);
places.map((place, index) => (
<Place
key={index}
place={place}
/>
));
Answer the question
In order to leave comments, you need to log in
Take a look at Cloud 9 Carousel - 3D Carousel for jQuery and change the image (and/or image classes like animated) with js events.
Calling the render function does not mean redrawing the DOM. React will figure out what needs to be changed.
If you want "super performance", then you need to change the store structure to something like this:
const initialState = {
result: [], // list of ids
data: {}, // map by id
}
export const placesReducer = (state, { type, payload }) => {
switch (type) {
case 'PLACES_LOAD_SUCCESS':
return {
...state,
// псевдокод, можно использовать https://github.com/paularmstrong/normalizr
result: payload.map(id),
data: payload.reduce(keyById),
}
}
}
// по этому результату делаете .map
export const getPlaces = state => state.places.result
// этот селектор будет дергать каждый Place, только ему нынче кидайте не place={place}, а id={id}, ибо .map по идентификаторам идёт
export const getPlace = (state, id) => state.places.data[id];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question