import React from 'react';
export const UserContext = React.createContext();
export default function App() {
return (
<UserContext.Provider value="Reed">
<User />
</UserContext.Provider>
)
}
function User() {
const value = React.useContext(UserContext);
return <h1>{value}</h1>;
}
const mycontext = createContext<myType>('default value')
import React, { useContext, createContext } from 'react'
const ThemeContext = React.createContext()
function Title() {
const theme = useContext(ThemeContext)
const style = {
background: theme.primary,
color: theme.text,
}
return <h1 style={style}>Title</h1>
}
function Nested() {
return <Title />
}
function NestedTwice() {
return <Nested />
}
export default function App() {
const theme = {
primary: 'dodgerblue',
text: 'white',
}
return (
<ThemeContext.Provider value={theme}>
<NestedTwice />
</ThemeContext.Provider>
)
}
import React from "react";
import ReactDOM from "react-dom";
// Create a Context
const NumberContext = React.createContext();
// It returns an object with 2 values:
// { Provider, Consumer }
function App() {
// Use the Provider to make a value available to all
// children and grandchildren
return (
<NumberContext.Provider value={42}>
<div>
<Display />
</div>
</NumberContext.Provider>
);
}
function Display() {
// Use the Consumer to grab the value from context
// Notice this component didn't get any props!
return (
<NumberContext.Consumer>
{value => <div>The answer is {value}.</div>}
</NumberContext.Consumer>
);
}
ReactDOM.render(<App />, document.querySelector("#root"));
//now with useContext the same page look like this
// import useContext (or we could write React.useContext)
import React, { useContext } from 'react';
// ...
function Display() {
const value = useContext(NumberContext);
return <div>The answer is {value}.</div>;
}
import React, { createContext } from 'react'
export const Context = createContext()
export const Provider = ({ children }) => {
const [state, setState] = useState({})
const value = {
state,
setState,
}
return <Context.Provider value={value}>{children}</Context.Provider>
}
import { createContext } from 'react';
const Context = createContext('Default Value');
import { Context } from '../context/ChatListContext'
class MainScreen extends Component {
static contextType = Context
constructor(props) {
super(props)
this.state = { loading: true, showAction: false }
setTimeout(() => {
StatusBar.setBackgroundColor(primary)
}, 100)
}
...
render() {
const {state, fetchChatList} =this.context;
}
}
import React from 'react'
import { Provider } from './Context'
import { ConsumingComponent } from './ConsumingComponent'
export const Page = () => {
return (
<div>
<Provider>
<ConsumingComponent />
</Provider>
</div>
)
}
import React, { useContext } from 'react'
import { Context } from './Context'
export const ConsumingComponent = () => {
const { state } = useContext(Context)
return null
}
function Main() {
const value = 'My Context Value';
return (
<Context.Provider value={value}>
<MyComponent />
</Context.Provider>
);
}