Abhishek Yadav
Redux
Redux
Props
State
The view
(DOM)
React is a library that helps keep your data in sync with the view
The Component
DOM changes when the state changes
DOM changes when the props change
Redux
React applications are a designed as a tree of components
Each component is designed to represent a region in the UI
Redux
Redux
// The Votes component
class Votes extends React.Component {
constructor(){
this.state = {
totalVotes: 0
}
}
render() {
//
}
}
The vote component
The vote component - next feature
The vote component
// The Votes component
class Votes extends React.Component {
constructor(){
this.state = {
totalVotes: 0
currentUserVote: 0; // or 1 or -1
}
}
render() {
//
}
}
The vote component
Since there are many answers on a page, its better to load all voting data together
The users voting data then becomes global with respect to the voting component
// The Votes component
class Votes extends React.Component {
constructor(props){
this.state = {
totalVotes: 0
}
}
render() {
<div>
{props.currentUserVote}
</div>
}
}
Pass votingData as props to the component
Let the parent component deal with fetching/managing the votingData
We just lifted state up.
The drill down
Global data gets attached to the root component, eventually - since it is used in many components
This data must be passed along the tree, as props, to reach where it is used
Functions are also passed similarly
The drill down
Root level data
The drill down
// App:
<div>
<Question
userData={this.state.userData}
userDataChange={this.state.handleChange}
/>
</div>
//Question:
<div>
<Vote
userData={this.props.userData}
userDataChange={this.props.userDataChange}
/>
</div>
We are passing lots of props to intermediate components
The drill down
Root level data
What we actually need
What we need
Good to have
The data we store in the store is often called state, because it can represents the state of the UI
State implementations
*Flux is actually the name of the pattern. It has implementations by the same name
Redux
store
component
reducers
actions
middlwares
connect
store
component
reducers
actions
middlwares
connect
watch changes,
update props
dispatch actions
observe actions
for side effects
Mutate store
on actions.
Be pure
on props change:
update UI
initiate action
on events
keep ui state
as data
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
The component side
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
The component side - subscribing
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
The component side - subscribing
const mapStateToProps = function(state){
return {
userVotes: state['currentUser']['votes']
}
}
connect(mapStateToProps, null)(Votes);
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
The component side - dispatching
The library = react-redux
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
The component side - dispatching
The library = react-redux
// action.js
function upVote(upOrDown) {
return {
type: UPVOTE,
upOrDown,
}
}
// Vote Component
handleUpVote(e) {
e.preventDefault();
this.props.dispatch(upVote(true))
}
// Connect
const mapDispatchToProps(dispatch) {
return dispatch;
}
connect(mapStateToProps, mapDispatchToProps)(Vote);
The component side - dispatching
// action.js
function upVote(upOrDown) {
return {
type: UPVOTE,
upOrDown,
}
}
// Vote Component
handleUpVote(e) {
e.preventDefault();
this.props.dispatch(countVote(true))
}
// Connect
const mapDispatchToProps(dispatch) {
return dispatch;
}
connect(
mapStateToProps,
mapDispatchToProps
)(Vote);
store
reducers
actions
Mutate store
on actions.
Be pure
keep ui state
as data
The store side
The store side - reducer
function voteDataReducer(state, action) {
switch (action.type) {
case UPVOTE:
return Object.assign({}, state,
{ userVote: userVote + action.upOrDown });
default:
return state;
}
}
actions
middlwares
observe actions
for side effects
The middleware
actions
middlwares
observe actions
for side effects
The middleware
//
The action
// App:
The problems
ROOT
___|___
/ / \ \
a b c d
root
a
b
c
d
x
y
m
n
The component
State
props
Root level data
Components that need the same data
component data
(state)
Data passed as props
Common parent component
Components that need the same data
state
Data passed as props
Common parent component
store
component
reducers
actions
middlwares
connect
store
component
reducers
actions
middlwares
connect
watch changes,
update props
dispatch actions
observe actions
for side effects
Mutate store
on actions.
Be pure
on props change:
update UI
initiate action
on events
keep ui state
as data
Redux
store
component
a
lot
of
other stuff
component
connect
watch changes,
update props
dispatch actions
on props change:
update UI
initiate action
on events
actions
middlwares
observe actions
for side effects
store
reducers
actions
Mutate store
on actions.
Be pure
keep ui state
as data