Connecting the App component

The only component that still needs to be connected now, is the App component. Here, we are going to inject the fetchTodos action creator, and update the component so that it uses the connected versions of all the other components.

Let's connect the App component now:

  1. Edit src/components/App.js, and adjust the following import statements:
import ConnectedAddTodo from '../containers/ConnectedAddTodo'
import ConnectedTodoList from '../containers/ConnectedTodoList'
import ConnectedTodoFilter from '../containers/ConnectedTodoFilter'
  1. Also, adjust the following components that are returned from the function:
            return (
<div style={{ width: 400 }}>
<Header />
<ConnectedAddTodo />
<hr />
<ConnectedTodoList />
<hr />
<ConnectedTodoFilter />
</div>
)
  1. Now, we can create the connected component. Create a new src/containers/ConnectedApp.js file.
  2. In this newly created file, we import the connect function from react-redux, and the bindActionCreators function from redux:
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
  1. Next, we import the fetchTodos action creator, and the App component:
import { fetchTodos } from '../actions'
import App from '../components/App'
  1. We already deal with the various parts of our state in other components, so there is no need to inject any state into our App component:
function mapStateToProps (state) {
return {}
}
  1. Then, we bind and return the fetchTodos action creator:
function mapDispatchToProps (dispatch) {
return bindActionCreators({ fetchTodos }, dispatch)
}
  1. Finally, we connect the App component and export it:
export default connect(mapStateToProps, mapDispatchToProps)(App)

Now, our App component is successfully connected to the Redux store.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.100