Last active
September 20, 2021 02:19
-
-
Save AD0791/075a12dade2a777629c4de2fa81b24a3 to your computer and use it in GitHub Desktop.
redux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INCREMENT = "INCREMENT"; // define a constant for increment action types | |
const DECREMENT = "DECREMENT"; // define a constant for decrement action types | |
// define the counter reducer which will increment or decrement the state based on the action it receives | |
const counterReducer = (state = 0, action) => { | |
switch (action.type) { | |
case INCREMENT: | |
return state + 1; | |
case DECREMENT: | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
// define an action creator for incrementing | |
const incAction = () => { | |
return { | |
type: INCREMENT | |
}; | |
}; | |
// define an action creator for decrementing | |
const decAction = () => { | |
return { | |
type: DECREMENT | |
}; | |
}; | |
// define the Redux store here, passing in your reducers | |
const store = Redux.createStore(counterReducer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ADD_TO_DO = "ADD_TO_DO"; | |
// A list of strings representing tasks to do: | |
const todos = [ | |
"Go to the store", | |
"Clean the house", | |
"Cook dinner", | |
"Learn to code" | |
]; | |
const immutableReducer = (state = todos, action) => { | |
switch (action.type) { | |
case ADD_TO_DO: | |
// don't mutate state here or the tests will fail | |
return state.concat(action.todo); | |
// or return [...state, action.todo] | |
default: | |
return state; | |
} | |
}; | |
// an example todo argument would be 'Learn React', | |
const addToDo = todo => { | |
return { | |
type: ADD_TO_DO, | |
todo | |
}; | |
}; | |
const store = Redux.createStore(immutableReducer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const immutableReducer = (state = ["Do not mutate state!"], action) => { | |
switch (action.type) { | |
case "ADD_TO_DO": | |
// don't mutate state here or the tests will fail | |
let arr = [...state, action.todo]; | |
return arr; | |
default: | |
return state; | |
} | |
}; | |
const addToDo = todo => { | |
return { | |
type: "ADD_TO_DO", | |
todo | |
}; | |
}; | |
const store = Redux.createStore(immutableReducer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const immutableReducer = (state = [0, 1, 2, 3, 4, 5], action) => { | |
switch (action.type) { | |
case "REMOVE_ITEM": | |
// don't mutate state here or the tests will fail | |
return [ | |
...state.slice(0, action.index), | |
...state.slice(action.index + 1, state.length) | |
]; | |
// or return state.slice(0, action.index).concat(state.slice(action.index + 1, state.length)); | |
default: | |
return state; | |
} | |
}; | |
const removeItem = index => { | |
return { | |
type: "REMOVE_ITEM", | |
index | |
}; | |
}; | |
const store = Redux.createStore(immutableReducer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment