1. What is the first principle of Redux?
Whether your app is a really simple or is a complex one, your app will be represented by a single JS object. s 2. What is a store and what do we use our reducers for within that store?
The store is where we hold the state of our application and we use our reducers to dispatch actions to our store to modify it.
3. Name three Redux store methods given to us by createStore and describe their use.
4. Explain to a non-technical recruiter what combineReducers()
does and why it is useful.
combineReducers()
allows devs to combine many reducing functions into one. It simplifies the organizational aspects of managing state.
1. Looking ahead at this module’s course schedule, What do you look forward to learning?
I’m looking forward to getting insight on redux from the instructor during lecture. It feels a bit overwhelming to just read docs.
2. What are your learning goals after reading and reviewing the class README?
My learning goal for this lesson is to know the differences in capability Redux and React’s own useReducer().
The abstract goal of Redux is to have a truly global, UI-agnostic application state and move closer to the ‘single-responsibility-principle’
reducer in a nutshell: function that return a brand-new-state
/* basic, vanilla Redux example */
'use strict';
// node way of importing redux
const { createStore } = require('redux');
// creating a store and using our initial state
const initialState = {
candidates: [
{ name: 'Rhea', votes: 0 },
{ name: 'Bernie', votes: 0 },
{ name: 'Santa', votes: 0 }
],
totalVotes: 0
};
// Vote action
const vote = (payload) => {
return {
type: 'VOTE',
payload: payload // 'Rhea', 'Bernie', 'Santa'
};
};
// the function that returns a brand `new` state
// when the reducer fires off, it gets two things:
// 1. the current `state`
// 2. and the action
const voteReducer = (state = initialState, action) => {
// look at the `action.type` to see what we want to do
// in this case we want to 'VOTE'
switch (action.type) {
case 'VOTE':
// find the candidate in the payuload
return {
// declare a new `candidates` thing that we'll make into the brand-new-state
candidates: state.candidates.map((candidate) => {
if (candidate.name === action.payload) {
candidate.votes = candidate.votes + 1;
}
// return the brand-new-state
return candidate;
}),
// increment votes
// increment total votes
totalVotes: state.totalVotes + 1
};
// return the current state if no relevant cases/candidates
default:
return state;
}
// we're performing the action on this value
//action.payload
};
const store = createStore(voteReducer);
console.log('store after creating: ', store);
store.dispatch(vote('Rhea'));
store.dispatch(vote('Santa'));
store.dispatch(vote('Bernie'));
// store should be updated with Rhea +1 votes
console.log(store.getState());
npm i
react react-redux
react-redux: gives us a provider
use createReducer function
test: import store
into jest file