Reactjs 如何在react with Typescript中使用childContextTypes?
我想使用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
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
}