React: Context 的几种用法

/*
 * @Desc: Context 的几种用法
 * @Author: Reda
 * @Date: 2023-08-12 17:48:17
 */

import React, { Component, createContext, useContext } from "react";

const AppContext = createContext();

/**
 * 1. Provider 和 Consumer 组合使用, Consumer中通过函数传参的形式 传值
 * 缺点:不易理解,多层嵌套中 不推荐
 */
class Head extends Component {
  render() {
    return (
      <AppContext.Consumer>{(value) => <div>{value}</div>}</AppContext.Consumer>
    );
  }
}

/**
 * 2. 结合Class组件的 contextType属性
 * Class组件的 contextType 属性可以赋值为 由React.createContext() 创建的Context对象
 * 此属性可以让你使用 this.context 来获取最近 Context上的值
 * 可以在 任何生命周期中访问到 this.context, 包括 render 函数
 */
class Content extends Component {
  static contextType = AppContext;

  render() {
    const value = this.context;

    return <div>{value}</div>;
  }
}

/**
 * 3. 函数组件 hooks 方式
 * 最简单
 */
const Foot = (params) => {
  const value = useContext(AppContext);
  return <div>{value}</div>;
};

class ContextUseDemo extends Component {
  render() {
    return (
      <AppContext.Provider value="Hello word!">
        <Head />
        <Content />
        <Foot />
      </AppContext.Provider>
    );
  }
}

export default ContextUseDemo;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React Hook中,子组件调用父组件函数的几种方法如下: 1. 通过props传递函数,子组件调用该函数实现调用父组件函数。 父组件代码: ```javascript import React from 'react'; import Child from './Child'; const Parent = () => { const handleClick = () => { console.log('Parent function called'); }; return ( <div> <Child handleClick={handleClick} /> </div> ); }; export default Parent;``` 子组件代码: ```javascript import React from 'react'; const Child = ({ handleClick }) => { const handleChildClick = () => { handleClick(); }; return ( <div> <button onClick={handleChildClick}>Call Parent Function</button> </div> ); }; export default Child; ``` 2. 使用useRef创建一个ref实例,并将其赋值给shareRef变量。通过指定ref为JSX属性,将其向下传递给子组件。子组件通过forwardRef获取父组件传递的ref,并使用useImperativeHandle将需要暴露给父组件的函数暴露出来。 父组件代码: ```javascript import React, { useRef } from 'react'; import Child from './Child'; const Parent = () => { const shareRef = useRef(); const handleParentClick = () => { shareRef.current.handleChildClick(); }; return ( <div> <button onClick={handleParentClick}>Call Child Function</button> <Child ref={shareRef} /> </div> ); }; export default Parent; ``` 子组件代码: ```javascript import React, { forwardRef, useImperativeHandle } from 'react'; const Child = forwardRef((props, ref) => { useImperativeHandle(ref, () => ({ handleChildClick: () => { console.log('Child function called'); }, })); return <div>Child Component</div>; }); export default Child; ``` 3. 使用Context API,将父组件的函数通过Context传递给子组件,子组件通过useContext获取父组件传递的函数并调用。 父组件代码: ```javascript import React, { createContext } from 'react'; import Child from './Child'; export const MyContext = createContext(); const Parent = () => { const handleClick = () => { console.log('Parent function called'); }; return ( <MyContext.Provider value={handleClick}> <Child /> </MyContext.Provider> ); }; export default Parent; ``` 子组件代码: ```javascript import React, { useContext } from 'react'; import { MyContext } from './Parent'; const Child = () => { const handleClick = useContext(MyContext); const handleChildClick = () => { handleClick(); }; return ( <div> <button onClick={handleChildClick}>Call Parent Function</button> </div> ); }; export default Child; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端卡卡西呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值