| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | title: Copy an Object with Object.assign | 
					
						
							|  |  |  | --- | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | # Copy an Object with Object.assign
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | --- | 
					
						
							|  |  |  | ## Problem Explanation
 | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | The goal of this challenge is to enforce state immutability when state is an object.  | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Hints
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | ### Hint 1
 | 
					
						
							|  |  |  | Use the method ```Object.assign({}, obj1, obj2)``` in return. Pass ```state``` as obj1.  | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | ### Hint 2
 | 
					
						
							|  |  |  | The obj2 should be the updated ```{key: value}``` pair of your state. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Solutions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <details><summary>Solution 1 (Click to Show/Hide)</summary> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | const defaultState = { | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  |   user: "CamperBot", | 
					
						
							|  |  |  |   status: "offline", | 
					
						
							|  |  |  |   friends: "732,982", | 
					
						
							|  |  |  |   community: "freeCodeCamp" | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const immutableReducer = (state = defaultState, action) => { | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  |   switch (action.type) { | 
					
						
							|  |  |  |     case "ONLINE": | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  |       // to enforce state immutability, return a new state object using Object.assign() method | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  |       return Object.assign({}, state, { status: "online" }); | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  |     default: | 
					
						
							|  |  |  |       return state; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const wakeUp = () => { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  |     type: "ONLINE" | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-03-02 04:23:43 +05:30
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const store = Redux.createStore(immutableReducer); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | </details> |