When should I send an asynchronous action to redux?

In different redux manuals, I have seen different approaches to submitting api actions.
For example, in the case of the async action of the reducex-reddit application, the function is componentDidMountsent:

class AsyncApp extends Component {
  constructor(props) {
    super(props)
    this.handleChange = this.handleChange.bind(this)
    this.handleRefreshClick = this.handleRefreshClick.bind(this)
  }

  componentDidMount() {
    const { dispatch, selectedSubreddit } = this.props
    dispatch(fetchPostsIfNeeded(selectedSubreddit))
  }
  ...

In the real world, an example of an async action is sent to a function componentWillMount:

function loadData(props) {
  const { login } = props
  props.loadUser(login, [ 'name' ])
  props.loadStarred(login)
}

class UserPage extends Component {
  constructor(props) {
    super(props)
    this.renderRepo = this.renderRepo.bind(this)
    this.handleLoadMoreClick = this.handleLoadMoreClick.bind(this)
  }

  componentWillMount() {
    loadData(this.props)
  }

So what is the right way to dispatch asynchronous actions? Or does it not matter? Also, as I know, in es6 classes the componentWillMountfunction is replaced with a constructor.

+4
source share
1 answer

( , ), , , componentDidMount, ajax , componentDidMount .

+1

All Articles