【问题标题】:Redux dispatch before rendering渲染前的 Redux 调度
【发布时间】:2016-12-03 23:21:09
【问题描述】:

我想在组件渲染之前调度一个动作。 但该操作是与 redux-saga 集成的异步操作。

我必须知道异步操作何时完成,如果完成,则渲染组件。 为了完成这项工作,我为每个容器设置了一个唯一的 ID,并且在操作完成后,属性 { loaded: true } 将被保存到存储中。

我是这样想的

@preload('uniqueId', (dispatch) => new Promise((resolve, reject) => {
  dispatch(MyAction(resolve, reject));
})
@connect(....)
class MyComponent extends React.Component {
  ....
}

@preload是在componenWillMount(服务器端)或componenDidMount(客户端)中触发指定动作的函数,当动作调用resolve()时,state.preloadState.uniqueId.loaded将设置为true .它还包装了组件,以便仅在 state.preloadState.uniqueId.loaded === true 时渲染组件。

@connect在指定action中将我要预加载的数据连接到redux store。

我想知道为 redux、redux-saga 应用程序和服务器端渲染进行数据预加载是否是常见的做法(我之前使用过 redux-async-connect,但我想让所有嵌套组件都能够做到数据预加载,所以我绑定到 componentWillMount 而不是一些静态函数)。

【问题讨论】:

    标签: reactjs redux redux-saga


    【解决方案1】:

    是的,你不能真正停止渲染组件(如果它的父级根本不渲染它),但你可以有条件地不渲染任何东西。比如:

    render() {
      if (!state.data) { return null; }
      return <div>{state.data}</div>
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多