scrimba
React Bootcamp Course
Plain JS Redux - Even more complex state
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Plain JS Redux - Even more complex state
AboutCommentsNotes
Plain JS Redux - Even more complex state
Expand for more info
index.js
run
preview
console
const redux = require("redux")

function changeCount(amount = 1) {
return {
type: "CHANGE_COUNT",
payload: amount
}
}

function addFavoriteThing(thing) {
return {
type: "ADD_FAVORITE_THING",
payload: thing
}
}

function removeFavoriteThing(thing) {
return {
type: "REMOVE_FAVORITE_THING",
payload: thing
}
}

const initialState = {
count: 0,
favoriteThings: []
}

function reducer(state = initialState, action) {
switch(action.type) {
case "CHANGE_COUNT":
return {
...state,
count: state.count + action.payload
}
case "ADD_FAVORITE_THING":
return {
...state,
favoriteThings: [...state.favoriteThings, action.payload]
}
case "REMOVE_FAVORITE_THING": {
const arrCopy = [...state.favoriteThings]

const updatedArr = state.favoriteThings.filter(thing => thing.toLowerCase() !== action.payload.toLowerCase())
return {
...state,
favoriteThings: updatedArr
}
}
default:
return state
}
}

const store = redux.createStore(reducer)
store.subscribe(() => {
console.log(store.getState())
})

// store.dispatch()
Console
/index.html
-6:51