React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)
How to dispatch an action on load?
mid LevelCode ExampleHooks
You can dispatch an action in componentDidMount() method and in render() method you can verify the data.
javascript
class App extends Component {
componentDidMount() {
this.props.fetchData();
}
render() {
return this.props.isLoaded ? (
<div>{"Loaded"}</div>
) : (
<div>{"Not Loaded"}</div>
);
}
}
const mapStateToProps = (state) => ({
isLoaded: state.isLoaded,
});
const mapDispatchToProps = { fetchData };
export default connect(mapStateToProps, mapDispatchToProps)(App);