Using Hooks for the App component

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:

  1. First delete the src/containers/ConnectedApp.js file.
  1. Now, edit the src/components/App.js file and import the useDispatch Hook from react-redux:
import { useDispatch } from 'react-redux'
  1. Additionally, import the fetchTodos action creator:
import { fetchTodos } from '../actions'
  1. Now, we can remove the props from the function definition:
export default function App () {
  1. Then, define the Dispatch Hook:
    const dispatch = useDispatch()
  1. Finally, adjust the Effect Hook and call dispatch():
    useEffect(() => {
dispatch(fetchTodos())
}, [ dispatch ])
  1. 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'
  1. 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.

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

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