我在想最近学习reactjs,希望有人能帮助我理解使用redux的情况,因为我对它的复杂逻辑感到困惑。使用redux有什么好处?
首先,我已经使用了angular2 +一段时间了,所以我也试图从我已经知道的东西中获得这个想法。
我见过很多redux的例子,但我不明白使用动作和分派的好处。
使用redux商店使用动作和缩减器而不是像这样使用类和主题有什么好处?
class Store{
subject = new BehaviorSubject(0);
get value(){
return this.subject.getValue();
}
inc(){
this.subject.next(this.subject.value+1);
}
dec(){
this.subject.next(this.subject.value-1);
}
clear(){
this.subject.next(0);
}
}
class Demo extends React.Component<{}, {}>{
store = new Store();
componentWillMount(){
this.store.subject.subscribe(_ => this.forceUpdate())
}
render(){
const value = this.store.value;
const { inc, dec, clear } = this.store;
const store = this.store;
return (
<div>
<span>{`current value is ${value}`}</span>
<button onClick={inc.bind(store)}>INC</button>
<button onClick={dec.bind(store)}>DEC</button>
<button onClick={clear.bind(store)}>CLEAR</button>
</div>
);
}
}
您是否得出结论,特别是关于Angular与Smart/DumbComponents或Services一起使用?据我了解,redux但Angular的好处可以做到这一点。我没有看到这一点,也许你现在知道更好.. – hogan
我认为至少对于一个个人项目来说,redux的好处是微乎其微的。我相信这对调试有用,但除此之外,我认为这对于涉及少数人的项目来说是一种矫枉过正。 – darwinsenior