React & Redux For Everyone

An Introduction to Redux
Redux is a predictable state container for JavaScript apps.
It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test.
Adding Redux & Our First Store
src/App.js

Our First Reducer & Actions
src/App.js

action is an object and describes what the action is supposed to do
the store then uses its dispatch method to call the action

you can see the above image, the reducer function does not modify state, it completely replaces the state tree
in order to nullify this, you need to spread in the state first in each return from the reducer
src/App.js

Properties On Actions & In Reducers
src/App.js

Adding Redux to Our React App
<Provider store={}></Provider>
must wrap your entry point into your app (ex: for this tutorial,
App.js
)this connects Redux to the react app using connect
makes grabbing props in and out of your components very easy and dispatching actions
has two parameters
store
children
create your store and pass it into the provider
src/App.js
Writing a Root Reducer
combines all reducers in the app into one reducer
combineReducers
src/rootRedcuer.js
src/reducer.js
Redux DevTools
download extension for browser
npm i redux-devtools-extension
in
App.js
import { composeWithDevTools } from 'redux-devtools-extension';
pass into createStore along with initial state of the store
const store = createStore(rootReducer, {}, composeWithDevTools());
composeWithDevTools
will not always be the thrid argument passed increateStore
, if your app will use middleware, then that gets passed in beforecomposeWithDevTools
Connecting to React
connect
allows to access your store in any given component easilyconnects Redux to React
src/Toggle.js
last line connects the component to the Redux store
now when the app uses the Toggle component, it will be using an enhanced version of it since Toggle has access to the Redux store
mapStateToProps()
- allows you to pick which pieces of your store your component will have access toas your app store scales, you don't need every component to access to the entire state tree
they should only have access to what they need and nothing more
src/App.js
src/reducer.js
src/rootReducer.js
Dispatching Actions from Components
src/reducer.js
src/Toggle.js
Toggle has access to dispatch because it is a prop of connect
in React dev Tools if you select the Toggle component, you will see in the props section
dispatch()

after adding an
onClick
event to the toggle button, have the arrow function returndispatch({type: 'TOGGLE_MESSAGE'})
, you will see below in the screen shot, the toggle action firesthis tells Redux on this event, dispatch 'TOGGLE_MESSAGE' action
the reducer receives an action of 'TOGGLE_MESSAGE'
the reducer then looks for that action type
once it finds the action type, it will return whatever that type says to return, in this instance initially it just returns state (we will actually have it return new state where it will toggle
messageVisibility
to the opposite of whatever it is currently in state as
modify the reducer to actually return what we want it to return,
{...state, messageVisibility: !state.messageVisibility}
Action Creators Explained
src/Toggle.js
as is right now, Toggle.js does not make much sense because the action and visibility state all live within the toggle component
the idea behind Redux to save the state in the global store if they are taking place outside the component
everytime you need an event to fire, do you want to have to create a function to dispatch the function? (like the
onClick
event in the avoe code)Hint: no
better to create a function 'toggleMessage' in a separate file, import that function into the component and then call that function instead
makes the action reusable
src/actions.js
src/Toggle.js
Bind Action Creators
eliminates the need to use dispatch within component
src/Toggle.js
const mapDispatchToProps = (dispatch) => bindActionCreators({ toggleMessage, }, dispatch);
basically takes the function toggleMessage and turns it into a prop - what this does it is bind dispatch (second argument) to the first argument (object of all actions for this particular component) - eliminates the need to pass dispatch into the component - just pass in toggleMessage instead
In the grand scheme of things, it may add some code, but it is much cleaner and easier to read
Action Type Constants
src/actions.js
used inside the reducer
src/reducer.js
prevent typo bugs and typing errors from going uncaught
example: if you mistype 'TOGGLE_MESSAGE' and did not use an action constant (exactly what we have been doing so far) it would still compile and render your app
you would not know you have any issue until you tried to use the toggle button
however, if you use an action constant and then mistype it, it will cause your app to fail instead
Adding Middleware Redux Logger
npm i redux-logger
src/App.js

Redux Thunks and API Calls for Actions
npm i redux-thunk
A thunk is a function that returns a function
Important because it allows us to return a function from an action
src/actions.js
src/App.js
src/reducer.js
src/Toggle.js
Loading Our Data with componentDidMount
src/features/movies/MoviesList.js
Loading State
src/features/movies/reducer.js
_src/features/movies/MoviesList.js
Resetting Our Store & Props in Actions
src/features/movies/reducer.js
src/features/movies/actions.js
src/features/movies/MovieDetail.js
Local Storage
npm i redux-localstorage-simple
not the only package that can be used
persists redux store to local storage
src/App.js
src/features/movies/reducer.js
src/features/movies/MoviesList.js