Documente Academic
Documente Profesional
Documente Cultură
index.js …
1 import React from
'react'
2 import { render } Add Todo
from 'react-dom'
3 import { Show: All Active
createStore } Completed
from 'redux'
4 import { Provider
} from Console
You can use Redux together with React, or with any other view library.
It is tiny (2kB, including dependencies).
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 1/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
render() {
const {user, orgs} = this.state;
return (
<div>
</div>
);
}
}
Why Redux?
React — A JS library that helps us to divide up our app into multiple
components but doesn’t clearly specify how to keep track of the
data(aka State) and how to deal with all the events(aka Actions)
properly.
React doesn’t talk about how to manage state object and redux is the
only way to fix it.
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 2/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
from components.
Three Principles
Redux can be described in three fundamental principles:
This makes it easy to create universal apps, as the state from your
server can be serialized and hydrated into the client with no extra
coding effort. A single state tree also makes it easier to debug or inspect
an application; it also enables you to persist your app’s state in
development, for a faster development cycle. Some functionality which
has been traditionally difficult to implement — Undo/Redo, for
example — can suddenly become trivial to implement, if all of your state
is stored in a single tree.
console.log(store.getState())
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 3/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
/* Prints
{
visibilityFilter: 'SHOW_ALL',
todos: [
{
text: 'Consider using Redux',
completed: true,
},
{
text: 'Keep all state in a single tree',
completed: false
}
]
}
*/
State is read-only
The only way to change the state is to emit an action, an object
describing what happened.
This ensures that neither the views nor the network callbacks will ever
write directly to the state. Instead, they express an intent to transform
the state. Because all changes are centralized and happen one by one in
a strict order, there are no subtle race conditions to watch out for. As
actions are just plain objects, they can be logged, serialized, stored, and
later replayed for debugging or testing purposes.
store.dispatch({
type: 'COMPLETE_TODO',
index: 1
})
store.dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: 'SHOW_COMPLETED'
})
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 4/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
Reducers are just pure functions that take the previous state and an
action, and return the next state. Remember to return new state
objects, instead of mutating the previous state. You can start with a
single reducer, and as your app grows, split it off into smaller reducers
that manage specific parts of the state tree. Because reducers are just
functions, you can control the order in which they are called, pass
additional data, or even make reusable reducers for common tasks such
as pagination.
That’s it! Now you know what Redux is all about.Now add redux to
react app.
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 5/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
We will have react app which will talk to redux for triggering action
and getting state object from it.
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 6/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 7/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
If we want to link our React application with the redux store, we first
have to let our app know that this store exists. This is where we come to
the first major part of the react-redux library, which is the Provider .
Since the provider only makes the store accessible to it’s children, and
we would ideally want our entire app to access the store, the most
sensible thing to do would be to put our App component within
Provider .
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 8/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
Complete picture
Connect
Now that we have “provided” the redux store to our application, we
can now connect our components to it. We established previously that
there is no way to directly interact with the store. We can either
retrieve data by obtaining its current state, or change its state by
dispatching an action (we only have access to the top and bottom
component of the redux flow diagram shown previously).
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 9/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
)
}
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 10/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
svg
It’s important to note that only components within the Provider can
be connected (In the above diagram, the connect is done through the
Provider).
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 11/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
Redux is a powerful tool and even more so when combined with React.
It really helps to know why each part of the react-redux library is
used, and hopefully after reading this post, the function of Provider
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 12/13
4/25/2019 Understanding Redux + React in Easiest Way Part-1 – Tarun Sharma – Medium
https://medium.com/@tkssharma/understanding-redux-react-in-easiest-way-part-1-81f3209fc0e5 13/13