Passing Data Between Adjacent Components
If you have components that are siblings and need to share data, the way to do that in React is to pull that data up into a parent component and pass it down with props.
That can be cumbersome though. Redux can help by giving you one global “parent” where you can store the data, and then you can connect
the sibling components to the data with React-Redux.
redux vs react-redux
redux
knows nothing about React at all. (The redux
library can be used outside of a React app too. It’ll work with Vue, Angular, and even backend Node/Express apps.)
react-redux
lets you connect pieces of the state to React components.
store
in charge for orchestrating all the interactions
the state is the data, and the store is where it’s kept.
reducer
the producer to make the state
(state, action) => newState
1 | var letters = ['r', 'e', 'd', 'u', 'x']; |
**Important Rule of Reducers **:
- Never return undefined from a reducer.
- Reducers must be pure functions.(This means that they can’t modify their arguments, and they can’t have side effects.)
action
plain JavaScript objects with a property named type.
An action object describes a change you want to make (like “please increment the counter”) or an event that happenend (like “the request to the server failed with this error”).
action don’t really do anything. Not on their own, anyway.
1 | { |
In order to make an action DO something, you need to dispatch it.
redux tips
Redux avoids these problems with some simple rules.
- State is read-only, and actions are the only way to modify it.
- Changes happen one way, and one way only: dispatch(action) -> reducer -> new state.
- The reducer function must be “pure” – it cannot modify its arguments, and it can’t have side effects.
The most important methods.
- getState for accessing the current state of the application
- dispatch for dispatching an action
- subscribe for listening on state changes
react-redux
Provider
By wrapping the entire app with the Provider
component, every component in the app tree will be able to access the Redux store if it wants to.
connect
all of components can access the Redux store, but not automatically , We’ll need to use the connect
function on our components to access the store.
connects a React component with the Redux store.
- the mapStateToProps function
- the mapDispatchToProps function
1 | import React, { Component } from "react"; |
redux-thunk
It is a middleware, basically a plugin for Redux, that enables Redux to deal with actions like getUser()
, ie.dispatch(getUser())
thunk: it’s an action creator that returns a function instead of a plain action object, like this:
1 | function doStuff() { |
the 2 agruments in return function,Most of the time you’ll only need dispatch
, but sometimes you want to do something conditionally, based on some value in the Redux state. In that case, call getState()
and you’ll have the entire state to read as needed.
setup redux thunk
1 | import thunk from 'redux-thunk'; |
redux-saga
redux-saga
relies on generators, but does a decent amount of the work for us, so (in my fairly limited experience) a deep understanding of them for this use-case isn’t necessary.
1 | import { takeLatest, call, put } from "redux-saga/effects"; |