Why doesn't it work like so:
class AppProvider extends React.Component {
state = {};
render (){
<React.Provider value={this.state}>{this.props.children}</React.Provider>
}
}
...
class AppConsumer extends React.Component {
state = {};
render (){
<React.Consumer>{context => context}</React.Consumer>
}
}
...
<AppProvider>
<AppConsumer>
<AppConsumer>
</AppConsumer>
</AppConsumer>
</AppConsumer>
<AppConsumer>
<AppConsumer>
</AppConsumer>
</AppProvider>
The only use I can think of for having the factory method is declaring a defaultValue
that consequent Consumers use, for testing etc. But, I think this is a bad pattern...
The confusing bit for me was, you can use the exact same context, but with two different values in 2 different places, so it's not really one shared context...
Aucun commentaire:
Enregistrer un commentaire