Next, we are going to update our App component so that it directly dispatches the fetchTodos action. Let's migrate the App component to Hooks now:
- First delete the src/containers/ConnectedApp.js file.
- Now, edit the src/components/App.js file and import the useDispatch Hook from react-redux:
import { useDispatch } from 'react-redux'
- Additionally, import the fetchTodos action creator:
import { fetchTodos } from '../actions'
- Now, we can remove the props from the function definition:
export default function App () {
- Then, define the Dispatch Hook:
const dispatch = useDispatch()
- Finally, adjust the Effect Hook and call dispatch():
useEffect(() => {
dispatch(fetchTodos())
}, [ dispatch ])
- Now, all that is left to do is to replace the ConnectedApp component with the App component in src/index.js. First, adjust the import statement:
import App from './components/App'
- Then, adjust the rendered component:
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
As we can see, using Hooks is much simpler and more concise than defining a separate container component.