react函数组件中使用context

效果

1.在父组件中创建一个createcontext并将他导出

import React, { createContext } from 'react'
import Bpp from './Bpp'
import Cpp from './Cpp'
export let MyContext = createContext('我是组件B')
export let Ccontext = createContext('我是组件C')
 
export default function App() {
    
    let a = '我是A组件传递的数据'
    let a1 = '我是A组件传递的二号数据'
    return (
        
            
                
context组件传值
) }

2.子组件先将父组件引入,然后通过usecontext方法进行接收

import React, { useContext } from 'react'
import {Ccontext} from './App'
 
export default function Cpp() {
    let Cdate=useContext(Ccontext)
    
  return (
    
{Cdate}
) }

你可能感兴趣的:(前端学习笔记,react.js,前端,前端框架)