jjzjj

childContextTypes

全部标签

javascript - 如何使用 flowtype 在组件的 react 上下文中定义 Prop ?

使用flowtype时在context中定义一个Prop这样//@flowtypeMyType=Object;classCustomViewextendsReact.Component{staticchildContextTypes={someProp:MyType}getChildContext(){return{someProp:this.props.someProp};}}我收到以下错误:CustomView:typespecificationofchildcontextsomePropsisinvalid;thetypecheckerfunctionmustreturnnull

javascript - ES6 中的子上下文类型

你会如何在ES6中编写对象childContextTypes?varA=React.createClass({childContextTypes:{name:React.PropTypes.string.isRequired},getChildContext:function(){return{name:"Jonas"};},render:function(){return;}}); 最佳答案 因为无论如何你都在使用Babel,所以你可以在你的代码中使用static(ES7),如下所示:exportdefaultclassAexte