我有一个具有顶级组件和许多嵌套子组件的React应用程序。顶级组件是唯一具有状态的组件。该状态采用单个变量形式:
this.state = {
g: new BlackJackGame()
}
g包含复杂的数据结构:
class BlackJackGame{
playerHand = [];
dealerHand = [];
deck = [ /* initially 52 cards * ];
....
}
g还包含三种突变就地状态的方法:
g.deal(): take 4 cards from deck and add them to playerHand and dealerHand (2 cards each)
g.hit(): take 1 card from deck and add it to playerHand
g.stay(): take cards from deck and add to dealerHand while dealerHand's points < 17.
onDealClick看起来像这样:
onDealClick = event => {
this.state.g.deal();
this.setState({ g: this.state.g });
};
我知道推荐的方法是不变异状态。但事实是,这非常有效。而且我多次使用过这种模式。它似乎工作得很好。据我所知,这些是唯一的缺点:
问题:我还缺少其他任何缺点吗?