• react useContext 用法


    1 创建createContext

    import React, { useContext, useEffect, useState } from 'react'

    const GlobalContext = React.createContext()

    GlobalContext 作为提供者

    export default function App(){

      const [filmList,setfilmList] = useState([]);

      const [info,setinfo] = useState('');

      useEffect(()=>{

        return()=>{

          axios.get(`/maizuo.json`).then(res=>{

            setfilmList(res.data)

          })

        }

      },[])

      return (

    //GlobalContext 作为提供者

        {

          call:'打电话',

          sms:'短信服务',

          info:info,

          changeInfo:(value)=>{

            setinfo(value)

          }

        }}>

         

            {

              filmList.map(item=>

               

              )

            }

           

         

       

      )

    }

    3 通过 useContext(GlobalContext) 可以直接只用提供者属性和方法

    function FilmItem(props){

      let { name, poster, grade,synopsis } = props;

      const value = useContext(GlobalContext)

      console.log(value,'--------123')

      return

    {

    //使用提供者方法

        value.changeInfo(synopsis)

       }}>

       

    {name}

       

         

         

    观众评分: {grade}

          alt

       

     

    }

    function FileDetail(){

      const value = useContext(GlobalContext);

      return

    //使用提供者属性

      detail=== { value.call } == { value.info }

    }

  • 相关阅读:
    Android 编译C++
    MongoDB简明手册
    C++模板——待决名
    从一坨代码说起
    【每日一题】74. 搜索二维矩阵
    Linux--exec族函数及与fork共用
    P272 小码君统统计字母频率-1
    记录将excel表无变形的弄进word里面来
    Docker swarm --集群和编排
    Redis订阅发布
  • 原文地址:https://blog.csdn.net/m0_65730686/article/details/136334306