scrimba
React Bootcamp Course
Plain JS Redux - Practice
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 - Practice
AboutCommentsNotes
Plain JS Redux - Practice
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
}
}

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]
}
default:
return state
}
}

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

store.dispatch(addFavoriteThing("Raindrops on roses"))
store.dispatch(addFavoriteThing("Whiskers on kittens"))

/**
* Challenge: implement an action creator called `removeFavoriteThing` which takes the string
* of the favorite thing you want to remove from the array and removes it
*/
store.dispatch(removeFavoriteThing("Raindrops on roses"))
Console
{count:
0
, favoriteThings:
[
"Raindrops on roses"
]
}
,
{count:
0
, favoriteThings:
[
"Raindrops on roses"
,
"Whiskers on kittens"
]
}
,
/index.html
-5:34