Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 未使用react中的上下文将值传递给子组件_Reactjs_React Context - Fatal编程技术网

Reactjs 未使用react中的上下文将值传递给子组件

Reactjs 未使用react中的上下文将值传递给子组件,reactjs,react-context,Reactjs,React Context,未为员工组件反映员工id const employeecontext= React.createContext(); export default class App extends React.Component { constructor(props) { super(props); this.state = { id: 101 }; } render() { return ( <div> &l

未为员工组件反映员工id

const employeecontext= React.createContext();

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: 101
    };
  }
  render() {
    return (
      <div>
        <h1>welcome to App</h1>
        <p>
          <label> Employee id: {this.state.id}</label>
        </p>
        <employeecontext.Provider value={this.state}>
          <Employee />
        </employeecontext.Provider>
      </div>
    );
  }
}
const employeecontext=React.createContext();
导出默认类App扩展React.Component{
建造师(道具){
超级(道具);
此.state={
身份证号码:101
};
}
render(){
返回(
欢迎使用应用程序

员工id:{this.state.id}

); } }
员工类别:

class Employee extends React.Component {
  static context = employeecontext;

  render() {
    return (
      <div>
        <h1>welcome to employee</h1>
        <p>
          <label> Employee id: {this.context.id}</label>
        </p>
      </div>
    );
  }
}
类Employee扩展React.Component{
静态上下文=employeecontext;
render(){
返回(
欢迎来到我们的员工

员工id:{this.context.id}

); } }
问题就在于这一行:

static context = employeecontext;
只需使用正确的静态类属性即可

像这样:

static contextType = employeecontext;