JavaScript react redux merge state with given array react redux merge state,react redux mergeprops,react redux mergeprops

Lets say I have a reducer which is like :

const initialState = [
  {
    accessToken: null,
    isLoggedIn: false,
  }
]

export default function my_reducer(state = initialState, action) {
  switch (action.type) {
    case LOGIN:
        return state.merge(user: action) ---> how to handle this 

and the output should be like:

[
      {
        accessToken: null,
        isLoggedIn: false,
        user: {
            name: 'some name',
            email: 'some email'
      }
    ]

In action I am getting a array which I am providing by doing JSON.stringify(response)

previous data should not be changed and new data should be updated

Answer:1

The ES6 Way

To create a new object with the state in ES6 we can use the spread operator. Like this

...
case ActionType.SUCCESS_GET_DATA : {
    let newState = { ...state, [action.uniqueKey]: action.payload };
    return state.merge(newState);
}
...

I did the uniqueKey part as a variable because you will want a unique name for your state.

IMO this syntax is much easier to understand than the Object.assign

Answer:2

You can use Object.assign() function:

var state = {
    accessToken: null,
    isLoggedIn: false,
};

var user = {
    name: 'some name',
    email: 'some email'
};

var newState = Object.assign({}, state, {user});

console.log(newState);
Answer:3

Using ES6 spread syntax

...
case 'ACTION_TYPE_A': {
return { ...state, action.key: action.value };
}
...

This will return the merged state by updating the 'key' if it exists in the original state.

Answer:4

I have a javascript function that display and hide a div after select. The div contains fields of a form. The div test2 has the fields of the div test1 plus other fields. All the fields are required. ...

I have a javascript function that display and hide a div after select. The div contains fields of a form. The div test2 has the fields of the div test1 plus other fields. All the fields are required. ...

  1. sap display hide field
  2. sharepoint display form hide field

Before upgrading to Extjs 6 we were using a tagfield component where its displayField was Ext.String.htmlEncode('description') where description was the description field of the store's record. This ...

Before upgrading to Extjs 6 we were using a tagfield component where its displayField was Ext.String.htmlEncode('description') where description was the description field of the store's record. This ...

How can I condition my text to become left-aligned if hits the end of its contained space and has to switch to the next line? Is there something inherent within CSS that can detect it, or does it have ...

How can I condition my text to become left-aligned if hits the end of its contained space and has to switch to the next line? Is there something inherent within CSS that can detect it, or does it have ...

  1. left align center text
  2. left align center html
  3. left align center
  4. left align div
  5. left align div css
  6. left align div bootstrap
  7. left align element css
  8. left align div content
  9. left align div html
  10. left align div elements
  11. left align element
  12. left align div tag
  13. left align element html
  14. align left center css
  15. align left center right on same line
  16. html left align centered text
  17. css left align centered text
  18. align left middle html
  19. align left middle
  20. float left align center

I am using the following regex in conjunction with abide Foundation for validating a password: <form id="setPassword" action="{{ url('changePasswordWithoutToken') }}" method="POST" class="account ...

I am using the following regex in conjunction with abide Foundation for validating a password: <form id="setPassword" action="{{ url('changePasswordWithoutToken') }}" method="POST" class="account ...

  1. regex password validation python
  2. regex password validation javascript
  3. regex password validation c#
  4. regex password validation java
  5. regex password validation codewars
  6. regex password validation no spaces
  7. regex password validation swift
  8. regex password validation 8 characters
  9. regex password validation php
  10. regex password validation python codewars
  11. regex password validation jquery
  12. regex password validation example
  13. regex password validation ruby
  14. regex password validation angular
  15. regex password validation
  16. regex password validation javascript code wars
  17. regex password validation pattern
  18. regex password validation mvc
  19. regex password validation powershell
  20. regex password validation oracle