Having problem in understanding REDUX code - reactjs

I am trying to create shopping basket through Redux Toolkit. I am finding it hard to understand this piece of code that what is purpose of all this code. Specifically those if conditions. Cant understand how add and remove reducer is working
const basketSlice = createSlice({
name: "basket",
initialState: INITIAL_STATE,
reducers: {
add: (state, action) => {
return state.map(item => {
if (item.id !== action.payload.id) {
return item
}
return {
...item,
added: true
}
})
},
remove: (state, action) => {
return state.map(item => {
if (item.id !== action.payload.id) {
return item
}
return {
...item,
added: false
}
})
}
}
})

You should check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Basically it loops over the state items, creating a new array from what is returned in each iteration of the loop.
So, what it does for, say, the remove reducer:
Loop over each item in state, each time returning something that will be an entry in the new array
The if section checks if the id of the current loop element is the same than the one we want to remove: if it's not the same ID, we return the item "as is", if it's the same ID, we return added: false so we know it was removed.
In the end, you get a new array that was processed through this map function, allowing to do whatever check you need to.
Say I have an array with 3 items:
const state = [
{ id: 12, name: "Fancy Phone", added: true, },
{ id: 54, name: "Leather Jacket", added: true, },
{ id: 564, name: "AI World-Dominating Robot", added: true, },
]
And I want to remove the "AI World-Dominating Robot" because I don't want anymore trouble:
// Create a new array from the .map
return state.map(item => {
// here we loop over each item one by one
// IF the ID in the action payload (thus the ID you want to remove) is not the same as the current item ID, we don't want to remove it
if (action.payload.id !== item.id) {
return item // so we return the item "as-is", and as we returned something, the .map loop moves to the next item
}
return { ...item, added: false } // otherwise, we set "added: false" to flag the fact it's removed

Related

React/Redux update state array if action id is not already present

I have a set of actions to update Redux state. I'm struggling filtering the end results. Here are my actions
GET_ITEMS (GET request, returns a list)
SET_ITEM (POST request, payload: 'id')
DELETE_ITEM (DELETE request, payload: 'id')
Here are my examples:
case 'GET_ITEMS':
return {
...state,
list: action.payload, // [{ id: 'one', count: 1 }, { id: 'two', count: 2}]
};
case 'SET_ITEM':
return {
...state,
list: [...state.list, { id: action.payload, count: state.list.length }],
};
case 'DELETE_ITEM':
return {
...state,
list: state.list.filter(item => item.id !== action.payload),
};
Only the GET_ITEMS action type returns a response data, the rest I have to manually update them. Bellow are my questions in the same order
How to update the state list array, compared to response data list array?
How to add an object item based on a id action payload?
Delete seems to be working fine
You can use a helper function for your SET_ITEM action:
case 'SET_ITEM':
return {
...state,
list: updateList(state.list, action)
};
function updateList(list, action) {
const found = list.find((element) => element.id === action.payload);
if (found) {
// ...update found props
return list;
}
return [...list, { id: action.payload, count: list.length + 1 }];
}
I would definitely move the count property outside of the element because the way it's modeled we'll have unnecessary count property for every element and if we remove an element in the middle of the array, the count property will be wrong for every element on the right side. So a better state would be:
{
...state,
list,
listCount
}

react redux keep same order when update

I've got a list of users
and in case of follow action
I've this slice:
usersFollowSuccess: (state, action) => {
const userId = action.payload._id;
const filtered = state.data.filter((user) => {
return user._id !== userId;
});
return {
...state,
data: [...filtered, action.payload],
error: false,
loaded: true,
};
},
All works fine but in the view the updated user
go at the last position.
How can I keep the same position in the view?
I've tried adding sort at the the end of map
with no luck.
UPDATE
I've worked out with
const data = state.data.map((user) => {
if (user._id === userId) {
user = action.payload;
}
return user;
});
Answer
First of all, If you want to use sort. It is exactly before to map like
items.sort(sortFunc).map(item=>(...));
You are mention I've tried adding sort at the the end of map with no luck. So, I guess you should sort item after render.
And, If you want to sort this Item at actions It is working
usersFollowSuccess: (state, action) => {
const userId = action.payload._id;
const filtered = state.data.filter((user) => {
return user._id !== userId;
});
return {
...state,
data: [...filtered, action.payload].sort(sortFunc), // Here
error: false,
loaded: true,
};
},
ETC
Don't be annoying cuz of rude people :)

Delete multiple item from array - Redux State

I'm working on react app with redux. I want to delete multiple item from array. I write below code in my reducer which delete single item from array but i want to delete multiple item.
case DELETE_LINK:
let dltLink = state.filter(item => {
return item._id !== action.data._id
})
return {
...state,
parentFolderlinks: dltLink
};
It seems you want to filter links from state.parentFolderlinks, say you have the ids in action.data.ids, you could
case DELETE_LINK:
const parentFolderlinks = state.parentFolderlinks.filter(item => {
return !action.data.ids.includes(item._id);
});
return {
...state,
parentFolderlinks
};
On what basis would you like to filter items? I assume that multiple items will not have the same id.
Below example shows how we can filter multiple items in redux. In this case, foods state with items that has type as fruit and removes everything else.
// initial state with all types of foods
const initialState = {
"foods": [
{
name: "apple",
type: "fruit"
},
{
name: "orange",
type: "fruit"
},
{
name: "broccoli",
type: "vegetable"
},
{
name: "spinach",
type: "vegetable"
},
]
}
// sample reducer that shows how to delete multiple items
export default (state = initialState, { type, payload }) => {
switch (type) {
// delete multiple items that does not have type fruit
// i.e both brocolli and spinach are removed because they have type vegetable
case DELETE_ITEMS_WITHOUT_TYPE_FRUIT:
const onlyFruits = state.foods.filter(food => food.type === "fruit");
return {
...state,
foods: onlyFruits
}
}
}
you could map over the state and run it through a function that works out if you want to keep it or not (I don't know what your logic is for that) then return the array at the end
const keepThisItem =(item) => {
return item.keep
}
case DELETE_LINK:
let itemsToKeep = []
let dltLink = state.map(item => {
if(keepThisItem(item){
itemsToKeep.push(item)
}
return itemsToKeep
})

Replace item in array with new value using dispatch in React

I've got an initial array, which can be added to and deleted from, no problems there..
const initialItems = [
{
id: Date.now(),
text: 'Get milk',
},
{
id: Date.now(),
text: 'Get eggs',
},
]
..but I'm trying to figure out how to edit the text effectively of one of the items using a dispatch function.
My dispatch looks like this:
const editItemHandler = () => {
dispatch({
type: 'EDIT_ITEM',
id: Date.now(),
text: itemInputRef.current.value,
index,
})
}
Which is just passing the value of an input
<input
autoFocus
type='text'
ref={itemInputRef}
onKeyDown={(e) => {
if (e.key === 'Escape') {
setToggle(!toggle)
}
if (e.key === 'Enter') {
// Dispatch
editItemHandler()
setToggle(!toggle)
}
}}
/>
My reducer file looks like this:
const itemReducer = (state, action) => {
switch (action.type) {
case 'ADD_ITEM': {
return [
...state,
{
id: action.id,
text: action.text,
},
]
}
case 'EDIT_ITEM': {
// Attempt 1
return [...state.splice((item, index) => index, 1, action.text)]
// Attempt 2
return [
...state.filter((item, index) => index !== action.index),
{
id: action.id,
text: action.text,
},
]
}
case 'DELETE_ITEM': {
return [...state.filter((item, index) => index !== action.index)]
}
default: {
return state
}
}
}
export default itemReducer
I've commented in 2 approaches I've already tried in the 'EDIT_ITEM' type.
Approach 1 just deletes the item and adds a new valued one albeit at the bottom of the array, which isn't what I want so I'd have to try and reorder after.
Approach 2 is using splice, which I thought was what would work for replacing an item with the specified value. However all it returns is ONLY the 'edited' with the original text (so not even edited), and deletes everything else.
How am I using this function incorrectly, or is there a better approach to editing an item in place? I'm obviously doing something wrong but can't figure out what. I've searched about and tried various approach to no avail.
Ideally I'd want the item to also keep the same id as before as well, so how to keep that would be a plus.
To update an item in an array you have several choices :
case 'EDIT_ITEM': {
// using map
return state.map((item, i) =>
i === action.index ? { id: action.id, text: action.text } : item
// using slice
return [
...state.slice(0, action.index),
{ id: action.id, text: action.text },
...state.slice(action.index+1)
]
This is an incorrect use of splice
return [...state.splice((item, index) => index, 1, action.text)]
because splice return an array containing the deleted elements, and it doesn't accept an function as first argument but the index at which to start changing the array.
the correct way with splice :
case 'EDIT_ITEM': {
// using splice
let newState = [ ...state ]
newState.splice(action.index, 1, { id: action.id, text: action.text })
// or you can directly do
newState[action.index] = { id: action.id, text: action.text }
// and return the new state
return newState;

Add value to array in reducer

I am trying to add to an array in my object, I'm passing the id and the value to add:
case ADD_ACHIEVEMENT:
return [
{
id: action.id,
achievements: [...state.achievements, action.label]
},
...state
]
Initial state:
const initialState = [
{
date: "Fri 1st",
enjoyments: [],
achievements: [],
id: 0
},
{
date: "Fri 2",
enjoyments: [],
achievements: [],
id: 1
},
How can I add the value in the reducer?
EDIT: Sorry I'm new to redux I might not be explaining myself properly. I'd like to update the achievements array for the id that I pass in the action. My achievements array is just an array of strings. I would just like to add another entry.
Getting sytax error:
Syntax error: C:/sites/CalendarRedux/src/reducers/days.js: Unexpected token, expected , (62:11)
case ADD_ACHIEVEMENT:
return state.map(day => {
if (day.id === action.id) {
return Object.assign({}, day, {
achievements: [
...day.achievements,
action.label,
]
}; <<<<<<< line 62
}
return day;
});
case ADD_ACHIEVEMENT:
return state.map(item => {
if (item.id === action.id) {
return Object.assign({}, item, {
achievements: [
...item.achievements,
action.label,
]
});
}
return item;
});
Given your comment you basically want to update the item in your array that matches the id passed by the action. This is one way to do it, basically you map each item to itself if you don't wanna update it, and you update the one that matches the id. The error you were making was trying to access state.achievements while instead you needed to access the achievements of the specific item.

Resources