2

I have a project, where I use react-redux, and I have a reducer, which by idea should add one element in array and return new array. How I can do this?

/*---- Reducer ----*/
case CHANGE_EVENT_USERS:
  return { ...state, users: payload };
 
/*---- Here's my hopeless tryings ----*/

userClickHandler() {
  const { id, homeFloor, avatarUrl, login } = this.props;
  const user = { id, homeFloor, avatarUrl, login };
  this.props.changeEventUsers([...user]); // []
  this.props.changeEventUsers(this.props.event.users.push()); // number
}

2
  • use push method for array Commented Jan 12, 2018 at 11:03
  • @Hushme, also tried Commented Jan 12, 2018 at 11:06

5 Answers 5

3

I'm not sure if I understand you correctly but from my understanding the solution to your problem would look something like this:

case CHANGE_EVENT_USERS:
  return { ...state, users: [ ...state.users, action.payload ] };
Sign up to request clarification or add additional context in comments.

Comments

2

I like better the syntax of concat. In your reducer do:

case CHANGE_EVENT_USERS:
  return users.concat(action.payload);

Comments

1

Do the add directly in the reducer.

From your component

this.props.changeEventUsers(newUser); // add the new user

In the reducer

return { ...state, users: [...state.users, payload] }; 

I made the assumption that "payload" contains the info coming from the action and the users array is by default initialised with an empty array value []

3 Comments

in initial state I assign hole array, that why I try don't mess with reducer
@RamzanChasygov try the above example. You should hold the entire array in reducer and only manipulate it there. That's the purpose of reducers. You shouldn't to manipulations inside the react components. So at the beginning of the reducer where you define the initial state define it with users: [], and subsequent additions should be done via the CHANGE_EVENT_USERS action
In initial state I need use object and users is one of this key
1

Use concat()

const initialArray = [1, 2, 3];  
const elemenToAdd = 4; 
const newArray= initialArray.concat([elementToAdd]);  

[1, 2, 3, 4]

Notice I'm using const here to emphasize that the initial array was not mutated.

The great thing about the method above, it's that it can be used to chain operations together.
result = initialArray.concat(..).filter(..).concat(..);
(where .. represents skipped code details)

You can also use concat by passing in arrays as parameters:
newArray = concat(initialArray, [elementToadd])

Or use es7 spread operator syntax for array concatenating:
newArray = [...initialArray, elementToAdd, ...[5], [6] ];

[1, 2, 3, 4, 5, [6] ]

Use ... to send individual elements of the supplied array to be elements in the new array; without the dots, the supplied array is itself the element.

So in the your case, the line in question could be written as:

Comments

0

I found my solution:

this.props.changeEventUsers([...this.props.event.users, user]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.