使用store来优化React组件的方法
在使用React编写组件的时候,我们常常会碰到两个不同的组件之间需要共享状态情况,而通常的做法就是提升状态到父组件。但是这样做会有一个问题,就是尽管只有两个组件需要这个状态,但是因为把状态提到了父组件,那么在状态变化的时候,父组件以及其下面的所有子组件都会重新render,如果你的父组件比较复杂,包含了其他很多子组件的话,就有可能引起性能问题。
Redux通过把状态放在全局的store里,然后组件去订阅各自需要的状态,当状态发生变化的时候,只有那些订阅的状态发生变化的组件才重新render,这样就避免了上面说的提升状态所带来的副作用。但是,当我们在写一个React组件库的时候,redux加react-redux的组合可能就有点太重了。所以我们可以自己写一个简单的store,来实现类似Redux的订阅模式。
参考Redux的实现来写一个简版的createStore:
functioncreateStore(initialState){
letstate=initialState;
constlisteners=[];
functionsetState(partial){
state={
...state,
...partial,
};
for(leti=0;i
我们的createStore非常简单,算上空行也只有33行,总共暴露了3个方法,没有Redux里的dispatch和reducer,直接通过setState方法改变状态。下面我们来用它一个计数器的例子。
classCounterextendsReact.Component{
constructor(props){
super(props);
//初始化store
this.store=createStore({
count:0,
});
}
render(){
return(
)
}
}
classButtonsextendsReact.Component{
handleClick=(step)=>()=>{
const{store}=this.props;
const{count}=store.getState();
store.setState({count:count+step});
}
render(){
return(
+
-
);
}
}
classResultextendsReact.Component{
constructor(props){
super(props);
this.state={
count:props.store.getState().count,
};
}
componentDidMount(){
this.props.store.subscribe(()=>{
const{count}=this.props.store.getState();
if(count!==this.state.count){
this.setState({count});
}
});
}
render(){
return(
{this.state.count}
);
};
}
例子中Buttons里通过store.setState来改变store中的状态,并不会引起整个Counter的重新render,但是因为Result中订阅了store的变化,所以当count有变化的时候就可以通过改变自己组件内的状态来重新render,这样就巧妙地避免了不必须要的render。
最后,上面的createStore虽然只有几十行代码,我还是把它写成了一个叫mini-store库放在GitHub上,并且提供了类似Redux的Provider和connect方法,总共加起来也就100多行代码。如果你也在写React组件库,需要管理一个复杂组件的状态,不妨试试这个优化方式。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。