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 { 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;
}
}
function Main() {
const value = 'My Context Value';
return (
<Context.Provider value={value}>
<MyComponent />
</Context.Provider>
);
}