react钩子_如何使用useContext React钩子

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教程以了解更多信息。

翻译自: https://flaviocopes.com/react-hook-usecontext/

react钩子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值