npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-redux-hk

v0.1.8

Published

Lightweight optimized alternative react-redux. Auto analyze dependent state to reduce re-render

Downloads

37

Readme

react-redux-hk

自动分析connect依赖的state,当且仅当mapStateToProps依赖的state改变时,才会重新计算mapStateToProps,进而触发重新渲染Component

完全兼容react-redux的api,可直接替代

解决什么问题

如下代码:

// 假定state定义如下
const state = {
    listData: [],
    planId: 0,
    unitId: 0,
    showEditor: false
};


function getInfo(state) {
    return {
        planId: state.planId,
        unitId: state.unitId
    };
}
function mapStateToProps(state) {
    return {
        planList: state.listData.filter(item => (item.unitList.length > 0)),
        info: getInfo(state)
    };
}

每次执行mapStateToProps返回的planList、info永远都是一个新的引用。 当showEditor改变,而mapStateToProps实际依赖的listData、planId、unitId没有变时, mapStateToProps仍然会重新执行,connect的Component也会重新渲染,而这些是不必要的性能损耗。

原理是mapStateToProps的返回值的某一项,值没有改,但是引用改了,导致react-redux的shallowEqual不一致, 进而会重新渲染connect的Component

业务代码中,这样的例子很常见,某一局部状态更新,牵一发而动全身,很多地方不需要重新渲染的,也重新渲染,大大降低页面性能

常见的方案

  • Immutable.js 引入这个库的成本很高,需要很多地方都改成Immutable.js的API
  • reselect 使用这个库也比较麻烦,因为需要显式地用function声明很多状态的依赖,导致mapStateToProps很复杂

react-redux-hk的思路

自动分析mapStateToProps依赖的state,只有当依赖的state改变时,才会重新计算mapStateToProps,进而触发重新渲染Component

注意

使用react-redux-hk替换react-redux时,要保证mapStateToProps是只依赖state和ownProps这两个变量的纯函数(可以依赖外部的常量)

如果mapStateToProps依赖其他变量(这是违反redux原则的,极力反对这样的代码),可以通过传第四个参数options.pureMapState = false来兼容

高级用法

react-redux-hk默认对redux的状态,分析两层(第一层reducer或model,第二层定义的状态字段),这是符合redux状态定义原则的,一般无需修改

如果状态的定义有过多的plain object(把一些子状态放到一同个状态对象下面,当然,一般是不推荐这种写法的) 可以通过传第四个参数options.depStateDepth = 3来调整分析依赖的深度,如果这个值设置得适当大一些,那么可能会找到mobX的影子

不需要额外处理,即可自动完成性能优化