Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukeghenco committed Feb 10, 2017
1 parent 504d5c8 commit dc33843
Showing 1 changed file with 67 additions and 74 deletions.
141 changes: 67 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,133 +1,126 @@
### Summary

When we last left off, we successfully used our createStore method, and integrated our the method into our react application to update our state. Unfortunately, our react application did not re-render in response to changes in the state. In this lesson, we'll fix that.
When we last left off, we successfully used our __createStore()__ method, and integrated our the method into our __React__ application to update our state. Unfortunately, our __React__ application did not re-render in response to changes in the state. In this lesson, we'll fix that.

## Use the Provider component from react-redux
## Use the Provider component from React Redux

The reason why the application did not re-render previously is because our react and redux library could not properly communicate to redux that a change in the store's state occurred. Luckily, we can use the react-redux library to get react and redux talking to one another. Run `npm install react-redux --save`.
The reason why the application did not re-render previously is because our __React__ and __Redux__ libraries could not properly communicate to each other correctly to specify that a change in the store's state occurred. Luckily, we can use the __React Redux__ library to get React and Redux talking to one another. Run `npm install react-redux --save` to install it and add to our `package.json`.

The react-redux library gives access to a method called the provider. The Provider is a component that comes from our react-redux library. It wraps around our App component. It does two things for us. The first is that it will alert our redux app when there has been a change in state, and this will re-render our redux app. Let's give it a shot.
The __React Redux__ library gives access to a component called the __Provider__. The __Provider__ is a component that comes from our __React Redux__ library. It wraps around our __App__ component. It does two things for us. The first is that it will alert our __Redux__ app when there has been a change in state, and this will re-render our __React__ app. Let's give it a shot.

Let's add the following code to our `src/index.js` file:

```javascript
// ./src/index.js

import React from 'react';
import ReactDOM from 'react-dom'
import configureStore from './store/configureStore';
import { Provider } from 'react-redux';
import {getShoppingListItems} from './actions/shoppingListItemActions'
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux'; /* code change */
import shoppingListItemReducer from './reducers/shoppingListItemReducer';
import App from './App';
import './index.css';

const store = configureStore();
const store = createStore(
shoppingListItemReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

ReactDOM.render(
<Provider store={store}>
<App store={store} />
</Provider>,
<App store={store}/>
</Provider>, /* code change */
document.getElementById('root')
);
```

We just did a few things:
We just did a few things here:

* We imported `Provider` from React-Redux
* We used `Provider` to wrap our react application
* We imported `Provider` from React Redux
* We used `Provider` to wrap our React application
* We passed our store instance into `Provider` as a prop, making it available to all of our other components.

### Step 2: Connecting The Container Component to Store

Using the `<Provider>` component provided by the react-redux, library we gave our components *the ability to be connected to the store*. However, we don't want every component re-rendering in response to every change in the state. So the react-redux requires us to specify which changes to the store's state should prompt a re-render of the application. We will specify this with the connect function.
Using the `<Provider>` component provided by the __React Redux__ library, we gave our components *the ability to be connected to the store*. However, we don't want every component re-rendering in response to every change in the state. So the __React Redux__ library requires us to specify which changes to the store's state should prompt a re-render of the application. We will specify this with the __connect()__ function.

#### Using the `connect` function
#### Using the `connect()` function

For a component to be connected to the store, i.e. to be able to get data from the store's internal state and to be told to re-render and get new data when that state changes, we will use the `connect` function made available to us by React-Redux.
For a component to be connected to the store, i.e. to be able to get data from the store's internal state and to be told to re-render and get new data when that state changes, we will use the __connect()__ function made available to us by React Redux.

Here's how it works:

Open up `src/App.js` and add the following:

```javascript
import { connect } from 'react-redux';
...


// export default App;

const connectedComponent = connect(mapStateToProps)(App)

function mapStateToProps(state){
return {items: state.items}
}

export default connectedComponent;
```

Holy cow those few lines are confusing. Let's see if we can understand them. Remember, that we have two goals here: (a) to only re-render our App component when specific changes to the state occur, and (b) to only provide the slice of the state that we need to our App component. So we will need (1) a function that listens to every change in the store and then (2) filters out the changes relevant to a particular component to (3) provide to that component. That's exactly what's happening here. In the next paragraph, let's go through what is doing what.

```javascript
const connectedComponent = connect(mapStateToProps)(App)
```

The connect function is taking care of task 1, it is synced up to our store, listening to each change in the state that occurs. When a change occurs, it calls a function *that we write* called mapStateToProps, and in mapStateToProps we specify exactly which slice of the state we want to provide to our component. Here, we want to provide state.items, and allow our component to have access to them through a prop called items. So that completes task 2. Then we have to say which component in our application we are providing this data to: you can see that we write `connect(mapStateToProps)(App)` to specify that we are connecting this state to the App component.

Finally this entire connect method returns a new component, it looks like the App component we wrote, but now it also receives the correct data. This is the component we wish to export. So at the bottom of the file, you see:
Open up `./src/App.js` and add the following:

```javascript
const connectedComponent = connect(mapStateToProps)(App)
// ./src/App.js

function mapStateToProps(state){
return {items: state.items}
}

export default connectedComponent;
```
*Note: We didn't have to import anything to define a `mapStateToProps` function! We wrote that function ourselves.*

Finally, in our mapStateToProps function we are saying that we are providing a new prop called items, so in our App component, that is the prop we want to reference. So the whole file looks like the following:

```javascript
import React, { Component } from 'react';
import { connect } from 'react-redux'; /* code change */
import './App.css';
import { connect } from 'react-redux';

class App extends Component {
handleOnClick(){
this.props.store.dispatch({type: 'GET_COUNT_OF_ITEMS'})

handleOnClick() {
this.props.store.dispatch({
type: 'INCREASE_COUNT',
});
}

render() {
return (
<div className="App">
<button onClick={this.handleOnClick.bind(this)}>Click</button>
<p> {this.props.items.length}</p>
<button onClick={(event) => this.handleOnClick()}>
Click
</button>
<p>{this.props.items.length}</p>
</div>
);
}
}
};

const connectedComponent = connect(mapStateToProps)(App)
// start of code change
const mapStateToProps = (state) => {
return { items: state.items };
};

function mapStateToProps(state){
return {items: state.items}
}
export default connect(mapStateToProps)(App);
// end of code change
```

export default connectedComponent;
Holy cow those last few lines are confusing. Let's see if we can understand them. Remember, that we have two goals here: (a) to only re-render our __App__ component when specific changes to the state occur, and (b) to only provide the slice of the state that we need to our __App__ component. So we will need (1) a function that listens to every change in the store and then (2) filters out the changes relevant to a particular component to (3) provide to that component. That's exactly what's happening here. In the next paragraph, let's go through what is doing what.

```javascript
export default connect(mapStateToProps)(App);
```

The connect function is taking care of task 1, it is synced up to our store, listening to each change in the state that occurs. When a change occurs, it calls a function *that we write* called __mapStateToProps()__, and in __mapStateToProps()__ we specify exactly which slice of the state we want to provide to our component. Here, we want to provide `state.items`, and allow our component to have access to them through a prop called items. So that completes task 2. Then we have to say which component in our application we are providing this data to: you can see that we write `connect(mapStateToProps)(App)` to specify that we are connecting this state to the __App__ component.
Finally this entire __connect()__ method returns a new component, it looks like the __App__ component we wrote, but now it also receives the correct data. This is the component we wish to export. So at the bottom of the file, you see:

```javascript
const mapStateToProps = (state) => {
return { items: state.items };
};

Ok, mapStateToProps and connect is very confusing, so we'll go dig through it some more. But for now, let's boot up our application, click the button, and see if we can finally get our application to render. Ok, it works - our component now properly re-renders!
export default connect(mapStateToProps)(App);
```

*Note: We didn't have to import anything to define a __mapStateToProps()__ function! We wrote that function ourselves.

Finally, in our __mapStateToProps()__ function we are saying that we are providing a new prop called items, so in our __App__ component, that is the prop we want to reference.

Ok, __mapStateToProps()__ and __connect()__ is very confusing, so we'll go dig through it some more. But for now, let's boot up our application, click the button, and see if we can finally get our application to render. Ok, it works - our component now properly re-renders!

###Summary

We learned of two new pieces of react-redux middleware: connect and Provider. The two pieces work hand in hand. Provider ensures that our entire react application can potentially access data from the store. Connect, allows us to specify which data we are listening to (through mapStateToProps), and which component we are providing the data. So when you see lines like
We learned of two new pieces of __React Redux__ middleware: __connect()__ and __Provider__. The two pieces work hand in hand. __Provider__ ensures that our entire React application can potentially access data from the store. Then __connect()__, allows us to specify which data we are listening to (through mapStateToProps), and which component we are providing the data. So when you see lines like this:

```javascript
connect(mapStateToProps)(App)
function mapStateToProps(state){
return {items: state.items}
}
const mapStateToProps = (state) => {
return { items: state.items };
};

connect(mapStateToProps)(App);
```

That is saying connect the data in mapStateToProps (the items portion of the state) to the App component. And the App component can access that state with as this.props.items. Don't fret if you still feel hazy on connect and mapStateToProps. It simply is confusing. We won't introduce any new material in the next code along, we'll just try to deepen our understanding of the material covered in this section. First, please take at least a 15 minute break before moving on.
That is saying connect the data in __mapStateToProps()__ (the items portion of the state) to the __App__ component. And the __App__ component can access that state with as this.props.items. Don't fret if you still feel hazy on __connect()__ and __mapStateToProps()__. This is a new middleware api that takes time to learn. We won't introduce any new material in the next code along, we'll just try to deepen our understanding of the material covered in this section. First, please take at least a 15 minute break before moving on.

0 comments on commit dc33843

Please sign in to comment.