mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-23 20:23:08 +00:00
Add mutliple contexts example
This commit is contained in:
36
examples/context/multiple-contexts.js
Normal file
36
examples/context/multiple-contexts.js
Normal file
@@ -0,0 +1,36 @@
|
||||
// Create a theme context, defaulting to light theme
|
||||
// highlight-next-line
|
||||
const ThemeContext = React.createContext('light');
|
||||
|
||||
// Signed-in user context
|
||||
const UserContext = React.createContext();
|
||||
|
||||
class App extends React.Component {
|
||||
static propTypes = {
|
||||
theme: PropTypes.string,
|
||||
signedInUser: PropTypes.shape({
|
||||
id: number,
|
||||
name: string,
|
||||
avatar: string,
|
||||
}),
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<ThemeContext.Provider value={this.props.theme}>
|
||||
<UserContext.Provider
|
||||
value={this.props.signedInUser}>
|
||||
<ThemeContext.Consumer>
|
||||
{theme => (
|
||||
<UserContext.Consumer>
|
||||
{user => (
|
||||
<ProfilePage user={user} theme={theme} />
|
||||
)}
|
||||
</UserContext.Consumer>
|
||||
)}
|
||||
</ThemeContext.Consumer>
|
||||
</UserContext.Provider>
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user