REACT Flashcards
What is Redux
Redux is a pattern and library that helps to managing and updating state in an application using events called “actions’. It is an open sours JS library for managing state in an application. It stores all necessary data in our application in a JS object separate from our application. We can grab this data for any component by connecting the component.
What is the Basic Idea Behind Redux
a single centralized place to contain global state in your application and specific patterns to follow when updating the state to make the code predictable.
State
Source of truth that drives our app
Actions
the events that occur in the app based on user input, and trigger updates in the state.
One-way data flow
- State describes condition of app at point in time
2. UI is revered based on that state Something happens (click) the state is updated based on what occurred
- The UI re-renders based on the new state
Immutability
can never be changed, in order to update code must make copies of existing obj/arrays and then modify the copies.
Actions
plain JS object that has a type field. (Event that describes something that happened in the application) type field should be a string that gives this action a descriptive name. Can have other fields with additional info about what happened by convention, put that info in a field called payload.
Action Creators
a function that create and returns an action object. Typically use these so we don’t have to write the action object by hand every time.
Reducers
function that receives the current state and an action object, decides how to update the state if necessary and returns the new state: (state, action) => newState (like an event listener which handles events based on the received action (event) type) get name bc similar to the kind of callback function you pass to the Array.reduce() method.
Reducers Rules
- Only calculate the new state value based on the state and action arguments
2. Not allowed to modify existing state, instead make immutable updates, by copying the existing state and making changes to the copied values.
3. Must not do any async logic, calculate random values, or cause side effects.
Reducer Flow
Check to see if reducer cares about this actions
If os make a copy of the state, update the copy with new values and return it
If not return the existing state unchanged.
Store
redux application state lives in an object called the store
created by passing in a reducer and has a method called get State that returned the current state value
dispatch
redux store has a method called dispatch. Only way to update the state is to call store.dispatch() and pass in an action object. The store will run its reducer function and save the new state value inside and we can gall get State() to retrace the updated value.
what are dispatching actions like
“triggering an event” in the application something happened and we want the store to know about it.
reducers act like …
act like event listeners and when they hear an action they are interested in they update the state in response.