我想使用像this page
这样的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>;
}
}
但我在页面中找不到任何内容!
答案 0 :(得分:2)
在类中,应使用=而不是:
设置childContextTypesstatic childContextTypes = {
foo: React.PropTypes.string
}