Are selectors better approach to get computed data, than class methods?

问题: I used in project react, redux and reselect. Is a preferred approach for apps, using reselect, to move all computable data from class methods to selectors and avoid mixing...

问题:

I used in project react, redux and reselect. Is a preferred approach for apps, using reselect, to move all computable data from class methods to selectors and avoid mixing of use both, or this approaches has different concepts?

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)

What is the best place for class methods, described below in component chunk? Please note, question about conepts and misconepts not about optimisations


回答1:

Reselect is used to get the computed data. It will calculate the value and cache it for the first time, when you will call it second time it will return you the cached value instead of performing the calculation again. Provided your state has not changed. If the value has change since the last cached it will perform the calculation again.

Where as if you will compute things in class methods they won't be cached and every time you will call class methods the calculation will happen again even if the values are not changed.

  • 发表于 2018-12-27 07:50
  • 阅读 ( 294 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除