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

83 lines
2.8 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.
*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
a component update whenever something changes, even if it's deeply nested.
Although the end product of this Codorial certain doesn't require Redux to function, as your app grows in complexity Redux becomes more and
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 (see below for explanation)
function reducer(state, action) {
return state;
}
export default createStore(reducer);
```
> You may want to consider installing the [redux-logger](https://github.com/evgenyrodionov/redux-logger) library.
### 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-02-28 17:46:38 +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.
2018-02-28 13:30:54 +00:00
- `action` is an object containing a `type`, and any unreduced data. More on this later.
## 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.
```js
// 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>
);
}
}
}
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!