在一个典型的 React 应用中,数据是通过 props 属性由上向下(由父及子)的进行传递的,如果组件套的很深的话,这样传递就非常恶心了。解决办法一般有两种:
1.Redux
把需要传递的数据放到store里,在需要用到的组件通过this.props获取
2.通过 Context 传递
传递属性或方法的爷爷组件
import React from 'react';
import PropTypes from 'prop-types';
export default class TeacherManageDetail extends React.PureComponent {
//第一步: 定义childContextTypes---子上下文类型
static childContextTypes = {
canEdit: PropTypes.bool,
};
//第二步:定义一个getChildContext用来返回上下文对象,返回的属性要和第一步定义类型对应
getChildContext() {
return {
canEdit: !this.isRole.isCurat