react-native-firebase/codorials/authentication-with-firebase/integrating-redux.md

85 lines
3.0 KiB
Markdown
Raw Normal View History

2018-02-28 13:30:54 +00:00
# Integrating Redux
Redux has become somewhat of a buzz word in the React community, and is generally used in most projects without thought. This Codorial
won't go into details on what it is as their own [documentation](https://redux.js.org/introduction/motivation) does a wonderful job at explaining
what it's for and why to use it.
2018-04-21 14:55:13 +00:00
_TLDR;_ Redux provides your app with a single "state" (data), which can be accessed by any component. You can subscribe to this data to cause
2018-02-28 13:30:54 +00:00
a component update whenever something changes, even if it's deeply nested.
2018-03-05 10:50:25 +00:00
Although the end product of this Codorial certainly doesn't require Redux to function, as your app grows in complexity Redux becomes more and
2018-02-28 13:30:54 +00:00
more important to manage your data.
## Installing Redux
Lets go ahead by installing the core Redux library and the React bindings:
2018-02-28 15:02:13 +00:00
```bash
2018-02-28 13:30:54 +00:00
npm install --save redux react-redux
```
Now within our projects `src` directory, create a `store.js` file. This file will contain all of our Redux logic, however you may want to break
this out into multiple directories as your projects grows in complexity.
```js
2018-02-28 13:30:54 +00:00
// src/store.js
import { createStore } from 'redux';
// Create a reducer with empty state (see below for explanation)
function reducer(state = {}, action) {
2018-02-28 13:30:54 +00:00
return state;
}
export default createStore(reducer);
```
2018-03-05 10:50:25 +00:00
By default state is `null`, however we're setting it to an empty `object` (`state = {}`) so we can attempt to access
shallow nested properties even if they don't exist.
2018-03-05 10:50:25 +00:00
> You may want to consider installing the [redux-logger](https://github.com/evgenyrodionov/redux-logger) library to improve
2018-04-21 14:55:13 +00:00
> your Redux experience.
2018-02-28 13:30:54 +00:00
### Reducer
A reducer is a simple JavaScript function which takes two arguments: `state` & `action`. The idea of a reducer is to take "some data" from an `action`
and return new state.
2018-04-21 14:55:13 +00:00
* `state` is any sort of data, which cannot be altered (immutable). A reducer must return a new value each time. More on this later.
* `action` is an object containing a `type`, and any unreduced data. More on this later.
2018-02-28 13:30:54 +00:00
## Integrating Redux into the app
Our Redux store is now ready to be used. `react-redux` provides us with a `Provider` component which "provides" any children
2018-02-28 17:46:38 +00:00
with access to the store via [context](https://reactjs.org/docs/context.html). Luckily we don't need to worry about this too much as the library
2018-02-28 13:30:54 +00:00
takes care of the hard work!
Back within our original bootstrap file, we'll wrap the `App` component in the `Provider` component, so our business logic has access to Redux.
2018-03-01 12:01:26 +00:00
```jsx
2018-02-28 13:30:54 +00:00
// src/index.js
import React, { Component } from 'react';
import { Provider } from 'react-redux'; // Import the Provider component
import App from './App';
import store from './store';
function bootstrap() {
// Init any external libraries here!
return class extends Component {
render() {
return (
<Provider store={store}>
<App />
</Provider>
);
}
2018-04-21 14:55:13 +00:00
};
2018-02-28 13:30:54 +00:00
}
export default bootstrap;
```
2018-03-01 10:03:55 +00:00
Although noting will visually change, our app now has access to the power of Redux!