react钩子
Check out my React hooks introduction first, if you’re new to them.
如果您是新手,请先查看我的React hooks简介 。
One React hook I sometimes use is useContext
.
我有时使用的一个React钩子是useContext
。
import React, { useContext } from 'react'
This hook is used in combination with the React Context API.
该钩子与React Context API结合使用。
In particular, this hook allows us to get the current context value:
特别是,此钩子使我们能够获取当前上下文值:
const value = useContext(MyContext)
which refers to the nearest <MyContext.Provider>
component.
指最近的<MyContext.Provider>
组件。
Calling useContext
will also make sure the component rerenders when the context value changes.
调用useContext
还将确保在上下文值更改时组件useContext
。
I recommend you to read my Context API tutorial to know more about it.
我建议您阅读我的Context API教程以了解更多信息。
react钩子