Adding Redux to react-native project Part 2
Background
Last post we introduced Redux to a blank react-native project primarily. In this post we’ll add Redux to a TODO list app, and also to implement a persistence feature to save added TODO item.
We borrow a well-written TODO list app as a starting point, in this way we’ll save a lot of energy to understand the mechanism of Redux.
Preparation
To start refactoring, first we need to install Redux and redux-persist dependencies.
Adding belowing modules into package.json dependencies:
1 | "react-redux": "^5.0.7", |
instead installation one by one, this time we install all in one command:
1 | $ npm install |
Snippets
- Change index.js, add Redux, persistence, App component wrapper :
1 | import { Provider } from 'react-redux'; |
Last post we explained the reason why using Provider outside of App. This example we added a NEW PersistGate to wrap App, enable it to automaticlly save the state to device. The persistor is a new stuff introduced later.
- Change App.js, connect component to Redux, deliver data by dispatch action :
1 | import { actionCreators } from './Redux' |
As you have seen, the big changes to this component is the way it process data, rather than using setState it use props.dispatch() . Where doest it come? The answer is in connect function:1
2
3
4
5
6
7
8
9
10// inject app state from redux, then add new property todos to App component
const mapStateToProps = (state, ownProps) => ({
todos: state.todos,
});
// Wrap App component with Connect component,
// and create interaction channel(props) for it.
const AppContainer = connect(
mapStateToProps
)(App);
in mapStateToProps function App component obtained a new property named ‘todos’, how to use it?
1 | render() { |
Again, we don’t use state anymore, using props instead!
- last, we have a look at new Redux.js
1 | // Initial state of the store |
We seperate app state/data into Redux, so, initial data moved to here from App.js. Also, actions definded here for simplification.
last but not at least is the store creation function:
1 | const _reducer = (state = initialState, action) => { |
the big difference from the last post is that we use persistedReducer to create store, and a new stuff persistor created as the same time.
Remember the usage of persistor? It’s used by PersistGate component in index.js.
So, that’s the main points we need to notice in a persistenc enchanced Redux RNA. The complete source code for this app is in here!
It can be used even in your daily life, isn’t it?
Enjoy!