代码之家  ›  专栏  ›  技术社区  ›  Andrew

选择器比类方法更适合获取计算数据吗?

  •  1
  • Andrew  · 技术社区  · 7 年前

    我用在了项目反应,重复和重新选择。对于使用重新选择的应用程序来说,将所有可计算数据从类方法移动到选择器并避免两者混合使用是一种首选方法,还是这种方法有不同的概念?

    class DocsListView {
      getOutdatedDocs() {
      // computations from props
      }
    
      getPrimaryDocs() {
      // computations from props
      }
    
      getDocs() {
        if(this.props.showOutdated) {
          return this.getOutdatedDocs();
        }
    
        return this.getPrimaryDocs();
      }
    
      render() {
        return (
          <DocsTable docs={this.getDocs()} />
        )
      }
    }
    
    export const DocsList = connect(createStructuredSelector({
      allDocs: allDocsSelector,
      showOutdated: outdatedFlagSelector,
      getRecordsFromSomeDictForDocsComparsion: dictSelector(DICT),
      // many Other Simple Selectors
    }))(DocsListView)
    

    下面组件块中描述的类方法的最佳位置是什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Reyansh Mishra    7 年前

    Reselect 用于获取计算数据。 它将计算值并第一次缓存它,当您第二次调用它时,它将返回缓存的值,而不是再次执行计算。前提是你的状态没有改变。如果该值自上次缓存后发生更改,则它将再次执行计算。

    当你在类方法中计算东西时,它们不会被缓存,每次你调用类方法时,即使值没有改变,计算也会再次发生。