Comment répandre l'état dans un tableau spécifique
const DataReducer = (state, action) => {
const { type, payload } = action;
switch (type) {
case 'ADD_DATA': {
const copy = [...state];
copy[0] = {
...copy[0],
data: [
...copy[0].data, {
id: Math.floor(Math.random() * 999),
name: 'Bob'
}
]
};
return copy;
}
}
}
const state = [{title: 'Names', data: []}, {title: 'Meal', data: []}];
const newState = DataReducer(state, { type: 'ADD_DATA', payload: { name: 'Bob' } });
console.log(newState);
Run code snippet
SAMER SAEID