How does reducer can work with an object with duplicate keys? - javascript

Learning React with Redux, the course author wrote this:
export default (state = [], action) => {
switch (action.type) {
case FETCH_STREAM:
return { ...stream, [action.payload.id]: action.payload };
case CREATE_STREAM:
return { ...stream, [action.payload.id]: action.payload };
case EDIT_STREAM:
return { ...stream, [action.payload.id]: action.payload };
default:
return state;
}
};
As much as I understood, the { ...stream, [action.payload.id]: action.payload } pattern is a new JavaScript syntax called "Key Interpolation Syntax" and what it does is that it adds a new key/value to our object ( which we have made a copy of it with spread ... syntax first ) so in that case I do understand the CREATE_STREAM one, but how about EDIT_STREAM ? Is it gonna replace the key because the key is already in there? or is it gonna blindly add it so then duplicate key? Also I don't understand the FETCH_STREAM at all. Why is it even like this? Shouldn't it just be a return action.payload ?

Actually, the Key Interpolation syntax will add that key if it doesn't exist but will override its value if it is duplicated.
The following code
const personA = {
name: 'John Doe',
age: 42
};
const createdProp = 'country';
const overriddenProp = 'age';
const personB = {
...personA,
[createdProp]: 'USA',
[overriddenProp]: 50
};
Will result in personB with these values
{
name: "John Doe",
age: 50,
country: "USA"
}
About the FETCH it depends on what they intended. If the spread operation is removed, then all previous data will be lost. If that is no problem, you can do { [action.payload.id]: action.payload } without worries.

Related

adding objects to collections elements using spread syntax

I'm working with react redux and want use spread syntax on a reducer in order to return and updated object which contains a collection element where I want to add objects.
I have the following reducer logic which is not working because the collection is being filled with the payload object values instead of the object itself:
export default function(state = INITIAL_STATE, action){
switch (action.type){
case CREATE:
return {...INITIAL_STATE,collection:{...state.collection,...action.payload}};
default:
return state;
}
}
The payload is an object like:
{
'DG67QBXQJW7':{
key:'12345',
price:'15',
name:'test'
}
};
And the returned state I would like to have something like:
{
name:'items',
collection:{
'DG67QBXQJW7':{
key:'12345',
price:'15',
name:'test'
},
'FB843VUV3N9':{
key:'67890',
price:'11',
name:'test2'
}
}
}
But I got with my previous showed logic something like:
{
name:'items',
key:'12345',
price:'15',
name:'test'
}
If someone could help or show me an advanced and good spread syntax guide for this kind of operations would be great.
Thanks in advance.
You just need to use your current state, not INITIAL_STATE, as the base of your object.
return {...state, collection: {...state.collection, ...action.payload}};
Test example:
const state = { name: 1, collection: { test3: { a: 3}, test4: {a: 4}}};
const payload = { test5: { a: 5 }};
console.log({...state, collection: {...state.collection, ...payload}});

Push to an array at a specific path of a useReducer's state

I have a (functional) component named Form, in which I used useReducer() hook to set up a state that looks like this:
{
content: {
profile: {
firstName: 'John',
lastName: 'Doe'
},
experiences: [
{
employer: 'CompanyX',
position: 'CEO'
},
{
employer: 'CompanyY',
position: 'Intern'
}
]
}
};
I want to push a new experience to the experiences array and I'm looking for the best way to write my reducer's case. Here's the solution I came up with:
// reducer.js
case 'ADD_EXPERIENCE': {
newState = _.clone(state);
newState.content.experiences = _.concat(state.content.experiences, action.newExperience);
return newState;
}
However, this doesn't look elegant to me. I was wondering if you guys have any better solution. E.g., is lodash's setWith() method appropriate?
// reducer.js
case 'ADD_EXPERIENCE': {
return _.setWith(
_.clone(state),
'content.experiences',
action.newExperience,
() => ???
)
}
You can use the ES6 spread operator ... to destructure the state and the new experiences array and return the new state, like this:
case 'ADD_EXPERIENCE': {
return {
...state,
content: {
...state.content,
experiences: [...state.content.experiences, action.newExperience]
}
}
}
Finally:
case 'ADD_EXPERIENCE':
return _.updateWith(
_.clone(state),
'content.experiences',
(value) => [...value, action.newExperience],
_.clone
);

Reducer - add new element

I have this store:
const initialActors = {
list: 'Actor\'s lists',
actors: [
{
name: 'Angelina Jole',
involved: true
},
{
name: 'Bratt Pitt',
involved: false
},
]
}
I have a reducer to add a new actor to my store:
const actors = (state = initialActors, action) => {
switch(action.type){
case 'ADD_NEW':
return {
...state.list,
actors: [
...state.actors,
action.name,
action.involved
]
}
default:
return state
}
}
I have a action creator too but it doesn't important. I dispatch that and display my state:
store.dispatch(addNew({name: "Hello", involved: true}) ); // I have this action creator
console.log(store.getState()
console.log displays very difficult object with letters like: {0: "A", 1: "c"}. What is wrong?
#Edit
I tryed change ...state.list to state list like this:
const actors = (state = initialActors, action) => {
switch(action.type){
case 'ADD_NEW':
return {
state.list,
actors: [
...state.actors,
action.name,
action.involved
]
}
default:
return state
}
}
but I have this error:
Parsing error: Unexpected token, expected ","
Similiar situation I have if I tryed modify actors array:
case 'ADD_NEW':
return {
...state.list,
actors: [
...state.actors,
{
action.name,
action.involved
}
]
}
Error message is this same but point at action object (action.name).
In your reducer, you are mistakenly spreading state.list. Since it is a string, you are getting all the letters. You should spread the state there. Because, you want to keep your state, other than actors array. This is why you spread the whole state and keep the list (with others if there would be).
Also, you are adding your item wrong, it should be an object.
const actors = (state = initialActors, action) => {
const { name, involved } = action;
switch (action.type) {
case "ADD_NEW":
return {
...state,
actors: [
...state.actors,
{
name,
involved
}
]
};
default:
return state;
}
};
state.list is a string, and you are trying to spread it
let a = 'name'
let c = {...a}
console.log(c)
run the above code snippet so you will be able to understand it
and for adding new object you need to update the reducer as follows
...state,
actors: [
...state.actors,
{
name: action.name,
involved: action.involved
}
]
so the above code will spread all the existing properties in the state and then you spread all the current actors and add a new object as shown above
Update
Since you are passing as
store.dispatch(addNew({name: "Hello", involved: true}) );
here the payload is an object so you can directly use that one
so in action it will be object which has two props one is
type
the payload you have send it
...state,
actors: [
...state.actors,
action.payload
]
Sample Working codesandbox
Just a small mistake what you did, you need to add as an object with 2 properties like [...state.actors, { action.name, action.involved }] instead of what you did.
From Spread syntax's documentation:
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Like this:
const actors = (state = initialActors, action) => {
switch(action.type){
case 'ADD_NEW':
return {
...state,
actors: [
...state.actors,
{
name: action.name,
involved: action.involved
}
]
}
default:
return state
}
}
Consider the following:
var array = [
{
name: 'someone',
involved: false,
}
];
console.log([...array, { name: 'hello', involved: true }]);
I hope this helps!

React Redux state not replacing

I am trying to fetch a new batch of products through an action and then replace the state with the new batch, however it just adds it to the array...
Here's my reducer for controlling the state:
const initialState = {
fetching: false,
fetched: false,
Brands:[],
Markings:[],
Products: [],
error: null
}
export default function reducer(state=initialState, action=null) {
switch (action.type){
case "FETCH_PRODUCTS_PENDING" : {
return {...state}
break;
}
case "FETCH_PRODUCTS_REJECTED" : {
return {...state}
break;
}
case "FETCH_PRODUCTS_FULFILLED" : {
return{ ...state,
Products: state.Products.concat(action.payload.data.Products),
Brands: action.payload.data.Facets[0].Facets,
Markings: action.payload.data.Facets[1].Facets
}
break;
}
}
return state
}
It goes wrong in the fulfilled case.. I am not sure how this "...state" works, do I need to do a object assign or something?
Upon load I get 52 products, when trying to request a new batch it adds it up so my this.props.products is 104 items... I want it to replace
This line is the culprit
Products: state.Products.concat(action.payload.data.Products),
In this case all you want to do is replace the Products array with the one from the action.
So it should be simply
Products: action.payload.data.Products
See here for a nice explanation on the spread operator:
https://ponyfoo.com/articles/es6-spread-and-butter-in-depth

How to update single value inside specific array item in redux

I have an issue where re-rendering of state causes ui issues and was suggested to only update specific value inside my reducer to reduce amount of re-rendering on a page.
this is example of my state
{
name: "some name",
subtitle: "some subtitle",
contents: [
{title: "some title", text: "some text"},
{title: "some other title", text: "some other text"}
]
}
and I am currently updating it like this
case 'SOME_ACTION':
return { ...state, contents: action.payload }
where action.payload is a whole array containing new values. But now I actually just need to update text of second item in contents array, and something like this doesn't work
case 'SOME_ACTION':
return { ...state, contents[1].text: action.payload }
where action.payload is now a text I need for update.
You can use map. Here is an example implementation:
case 'SOME_ACTION':
return {
...state,
contents: state.contents.map(
(content, i) => i === 1 ? {...content, text: action.payload}
: content
)
}
You could use the React Immutability helpers
import update from 'react-addons-update';
// ...
case 'SOME_ACTION':
return update(state, {
contents: {
1: {
text: {$set: action.payload}
}
}
});
Although I would imagine you'd probably be doing something more like this?
case 'SOME_ACTION':
return update(state, {
contents: {
[action.id]: {
text: {$set: action.payload}
}
}
});
Very late to the party but here is a generic solution that works with every index value.
You create and spread a new array from the old array up to the index you want to change.
Add the data you want.
Create and spread a new array from the index you wanted to change to the end of the array
let index=1;// probably action.payload.id
case 'SOME_ACTION':
return {
...state,
contents: [
...state.contents.slice(0,index),
{title: "some other title", text: "some other text"},
...state.contents.slice(index+1)
]
}
Update:
I have made a small module to simplify the code, so you just need to call a function:
case 'SOME_ACTION':
return {
...state,
contents: insertIntoArray(state.contents,index, {title: "some title", text: "some text"})
}
For more examples, take a look at the repository
function signature:
insertIntoArray(originalArray,insertionIndex,newData)
Edit:
There is also Immer.js library which works with all kinds of values, and they can also be deeply nested.
You don't have to do everything in one line:
case 'SOME_ACTION': {
const newState = { ...state };
newState.contents =
[
newState.contents[0],
{title: newState.contents[1].title, text: action.payload}
];
return newState
};
I believe when you need this kinds of operations on your Redux state the spread operator is your friend and this principal applies for all children.
Let's pretend this is your state:
const state = {
houses: {
gryffindor: {
points: 15
},
ravenclaw: {
points: 18
},
hufflepuff: {
points: 7
},
slytherin: {
points: 5
}
}
}
And you want to add 3 points to Ravenclaw
const key = "ravenclaw";
return {
...state, // copy state
houses: {
...state.houses, // copy houses
[key]: { // update one specific house (using Computed Property syntax)
...state.houses[key], // copy that specific house's properties
points: state.houses[key].points + 3 // update its `points` property
}
}
}
By using the spread operator you can update only the new state leaving everything else intact.
Example taken from this amazing article, you can find almost every possible option with great examples.
This is remarkably easy in redux-toolkit, it uses Immer to help you write immutable code that looks like mutable which is more concise and easier to read.
// it looks like the state is mutated, but under the hood Immer keeps track of
// every changes and create a new state for you
state.x = newValue;
So instead of having to use spread operator in normal redux reducer
return {
...state,
contents: state.contents.map(
(content, i) => i === 1 ? {...content, text: action.payload}
: content
)
}
You can simply reassign the local value and let Immer handle the rest for you:
state.contents[1].text = action.payload;
Live Demo
In my case I did something like this, based on Luis's answer:
// ...State object...
userInfo = {
name: '...',
...
}
// ...Reducer's code...
case CHANGED_INFO:
return {
...state,
userInfo: {
...state.userInfo,
// I'm sending the arguments like this: changeInfo({ id: e.target.id, value: e.target.value }) and use them as below in reducer!
[action.data.id]: action.data.value,
},
};
Immer.js (an amazing react/rn/redux friendly package) solves this very efficiently. A redux store is made up of immutable data - immer allows you to update the stored data cleanly coding as though the data were not immutable.
Here is the example from their documentation for redux:
(Notice the produce() wrapped around the method. That's really the only change in your reducer setup.)
import produce from "immer"
// Reducer with initial state
const INITIAL_STATE = [
/* bunch of todos */
]
const todosReducer = produce((draft, action) => {
switch (action.type) {
case "toggle":
const todo = draft.find(todo => todo.id === action.id)
todo.done = !todo.done
break
case "add":
draft.push({
id: action.id,
title: "A new todo",
done: false
})
break
default:
break
}
})
(Someone else mentioned immer as a side effect of redux-toolkit, but you should use immer directly in your reducer.)
Immer installation:
https://immerjs.github.io/immer/installation
This is how I did it for one of my projects:
const markdownSaveActionCreator = (newMarkdownLocation, newMarkdownToSave) => ({
type: MARKDOWN_SAVE,
saveLocation: newMarkdownLocation,
savedMarkdownInLocation: newMarkdownToSave
});
const markdownSaveReducer = (state = MARKDOWN_SAVED_ARRAY_DEFAULT, action) => {
let objTemp = {
saveLocation: action.saveLocation,
savedMarkdownInLocation: action.savedMarkdownInLocation
};
switch(action.type) {
case MARKDOWN_SAVE:
return(
state.map(i => {
if (i.saveLocation === objTemp.saveLocation) {
return Object.assign({}, i, objTemp);
}
return i;
})
);
default:
return state;
}
};
I'm afraid that using map() method of an array may be expensive since entire array is to be iterated. Instead, I combine a new array that consists of three parts:
head - items before the modified item
the modified item
tail - items after the modified item
Here the example I've used in my code (NgRx, yet the machanism is the same for other Redux implementations):
// toggle done property: true to false, or false to true
function (state, action) {
const todos = state.todos;
const todoIdx = todos.findIndex(t => t.id === action.id);
const todoObj = todos[todoIdx];
const newTodoObj = { ...todoObj, done: !todoObj.done };
const head = todos.slice(0, todoIdx - 1);
const tail = todos.slice(todoIdx + 1);
const newTodos = [...head, newTodoObj, ...tail];
}
Pay attention to the data structure:
in a project I have data like this
state:{comments:{items:[{...},{...},{...},...]} and to update one item in items I do this
case actionTypes.UPDATE_COMMENT:
const indexComment = state.comments.items.findIndex(
(comment) => comment.id === action.payload.data.id,
);
return {
...state,
comments: {
...state.comments,
items: state.comments.items.map((el, index) =>
index === indexComment ? { ...el, ...action.payload.data } : el,
),
},
};
Note: in newer versions (#reduxjs/toolkit), Redux automatically detects changes in object, and you don't need to return a complete state :
/* reducer */
const slice = createSlice({
name: 'yourweirdobject',
initialState: { ... },
reducers: {
updateText(state, action) {
// updating one property will cause Redux to update views
// only depending on that property.
state.contents[action.payload.id].text = action.payload.text
},
...
}
})
/* store */
export const store = configureStore({
reducer: {
yourweirdobject: slice.reducer
}
})
This is how you should do now.

Categories

Resources