Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 with Typescript中使用childContextTypes?_Reactjs_Typescript - Fatal编程技术网

Reactjs 如何在react with Typescript中使用childContextTypes?

Reactjs 如何在react with Typescript中使用childContextTypes?,reactjs,typescript,Reactjs,Typescript,我想使用childContextTypes,比如 我用Typescript更改它: import * as React from "react"; export class Parent extends React.Component<{}, {}> { childContextTypes: { foo: string } getChildContext() { return { foo: "I m the grandpar

我想使用childContextTypes,比如

我用
Typescript
更改它:

import * as React from "react";

export class Parent extends React.Component<{}, {}> {
    childContextTypes: {
        foo: string
    }

    getChildContext() {
        return { foo: "I m the grandparent" };
    }

    render() {
        return <Child />
    }
}

class Child extends React.Component<{}, {}> {
    context: any;

    static contextTypes: {
        foo: string
    }

    render() {
        return <div>Hello {this.context["foo"]}</div>;
    }
}
import*as React from“React”;
导出类父类扩展React.Component{
childContextTypes:{
foo:string
}
getChildContext(){
return{foo:“我是祖父母”};
}
render(){
返回
}
}
子类扩展了React.Component{
背景:任何;
静态上下文类型:{
foo:string
}
render(){
返回Hello{this.context[“foo”]};
}
}

但我在页面中找不到任何内容

在类中,应使用=而不是:

static childContextTypes = {
    foo: React.PropTypes.string
}