0 votes
in ReactJS by

How to dispatch an action on load?

1 Answer

0 votes
by

You can dispatch an action in componentDidMount() method and in render() method you can verify the data.

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)

Related questions

0 votes
asked Dec 22, 2023 in ReactJS by john ganales
+1 vote
asked Apr 3, 2023 in QuickBase by Robin
...