Javascript 我应该何时在redux中调度异步操作?

Javascript 我应该何时在redux中调度异步操作?,javascript,reactjs,redux,Javascript,Reactjs,Redux,在不同的redux指南中,我看到了调度api操作的不同方法。 例如,在redux reddit应用程序中,异步操作在componentDidMount函数中调度: class AsyncApp extends Component { constructor(props) { super(props) this.handleChange = this.handleChange.bind(this) this.handleRefreshClick = this.handl

在不同的redux指南中,我看到了调度api操作的不同方法。
例如,在redux reddit应用程序中,异步操作在
componentDidMount
函数中调度:

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))
  }
  ...
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)
  }
在实际示例中,异步操作在
componentWillMount
函数中调度:

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))
  }
  ...
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)
  }

那么,调度异步操作的正确方法是什么呢?还是没关系?另外,正如我所知,在es6类中,
组件将挂载
函数被构造函数替换。

如果您正在构建同构应用程序(客户端和服务器呈现),您倾向于使用
componentDidMount
,因为您只想从客户端发出ajax调用,而
componentDidMount
只在客户端上运行。

这取决于您的情况,两者都可以。
componentDidMount
componentWillMount
之间的区别是:1、
componentDidMount
componentWillMount
之后运行;2、
componentDidMount
仅在客户端运行。看见