- 在createContext()時初始化
- context.provider value = {state}時給值
//在createContext()時初始化 import React from 'react' const state = { name: 'Tania', loggedIn: true } const UserContext = React.createContext(state)
//context.provider value = {state}時給值 import React from 'react' import HomePage from './HomePage' import { UserProvider } from './UserContext' function App() { const user = { name: 'Tania', loggedIn: true } return ( <UserProvider value={user}> <HomePage /> </UserProvider> ) }
@copyright MRcodingRoom
觀看更多文章請點MRcoding筆記
觀看更多文章請點MRcoding筆記