博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react Context
阅读量:4111 次
发布时间:2019-05-25

本文共 877 字,大约阅读时间需要 2 分钟。

1、 父组件

父组件:import PropTypes from 'prop-types'  // 声明Context对象属性  static childContextTypes = {    propA: PropTypes.string,    methodA: PropTypes.func  }    // 返回Context对象,方法名是约定好的  getChildContext () {    return {      propA: 'propA',      methodA: () => 'methodA'    }  }子组件:Ï // 为了在子组件中抓取context, 我们需要告诉React我们想要访问context    // 我们通过在子组件中定义 contextTypes 和 React交流    static contextTypes = {        users: PropTypes.array    }    render(){    const {users} = this.context    }

 方式2:react16.3之后有了新的方法

父组件// export const { Provider, Consumer } = React.createContext("默认名称");

父组件定义的值:{camera}

Ï自组件import { Consumer } from "./index";//引入父组件的Consumer容器
{(context) =>

子组件。获取父组件的值:{context}

{/* 孙组件内容 */}
}

 

转载地址:http://qemsi.baihongyu.com/

你可能感兴趣的文章
线段树总括
查看>>
线段树模板
查看>>
线段树的应用方法总结
查看>>
线段树--HDU--1166
查看>>
线段树--HDU--1754
查看>>
线段树应用
查看>>
uva11491
查看>>
uva1611
查看>>
单源最短路——Dijstra
查看>>
Dijkstra算法 Atcoder-070-D
查看>>
求N个数的最小公倍数
查看>>
时间复杂度分析
查看>>
CodeForces-428-div2-c
查看>>
OpenJ_POJ 1043
查看>>
uva11134 贪心
查看>>
C语言中的atan 与atan2
查看>>
计算机中叉积
查看>>
uva1606
查看>>
Codeforces-429-2-A Generous Kefa
查看>>
Codeforces-429-2-B Godsend
查看>>