Added hints and solutions to Redux exercises (#35244)

* Added hints and solutions to Redux exercises

solution to Redux exercise: Copy an Object with Object.assign

* Added the full code for the solution
This commit is contained in:
ramyasree
2019-03-02 04:23:43 +05:30
committed by Randell Dawson
parent 46c4263931
commit 6dbec7e44f

View File

@ -3,8 +3,39 @@ title: Copy an Object with Object.assign
---
## Copy an Object with Object.assign
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/front-end-libraries/redux/copy-an-object-with-object.assign/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
The goal of this challenge is to enforce state immutability when state is an object.
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
### Hint 1
Use the method ```Object.assign({}, obj1, obj2)``` in return. Pass ```state``` as obj1.
### Hint 2
The obj2 should be the updated ```{key: value}``` pair of your state.
### Solution
```
const defaultState = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
community: 'freeCodeCamp'
};
const immutableReducer = (state = defaultState, action) => {
switch(action.type) {
case 'ONLINE':
// to enforce state immutability, return a new state object using Object.assign() method
return Object.assign({}, state, {status: 'online'});
default:
return state;
}
};
const wakeUp = () => {
return {
type: 'ONLINE'
}
};
const store = Redux.createStore(immutableReducer);
```
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->