[英]propTypes isRequired constraint mis-leading behaviour in react
I'm very new to react( or to say web technologies). 我很反应(或说网络技术)。 I started building an app which uses different components.
我开始构建一个使用不同组件的应用程序。 When I went through the documentation, I thought putting
isRequired
in propTypes, constrains the user to provide all isRequired
attributes when using the component. 当我浏览文档时,我想在
isRequired
中放置isRequired,约束用户在使用组件时提供所有isRequired
属性。
But, in this example even if I don't pass isRequired
attributes I'm able to load the component. 但是,在这个例子中,即使我没有传递
isRequired
属性,我也可以加载组件。
var PanelPreview = React.createClass({
getInitialState: function(){
return { captionIndex: 0 };
},
propTypes: {
beforeSrc: React.PropTypes.string.isRequired,
afterSrc: React.PropTypes.string.isRequired,
captionTable: React.PropTypes.array
},
});
module.exports = PanelPreview;
app using this component: 使用此组件的应用:
React.render(
< PanelPreview />,
document.getElementById('main')
);
I want to constrain the user of the component to provide these two values else throw some sort of error. 我想约束组件的用户提供这两个值,否则会抛出某种错误。
Props failing validation isn't currently considered a critical error. 道具失败验证目前不被视为严重错误。 However there will be a warnning logged in the console:
但是,控制台中会记录警告:
Warning: Required prop `beforeSrc` was not specified in `PanelPreview`.
This only happens in a non-production build of react. 这只发生在非生产版本的反应中。 In production nothing is logged.
在生产中没有记录。 See this fiddle for an example http://jsfiddle.net/5mafp3eu/1/
请看这个小提琴,例如http://jsfiddle.net/5mafp3eu/1/
There has been discussion about having React fail-fast for prop validation failures (see issues below), it seems like it's going to happen eventually, but I don't think it's on any roadmap yet. 有人讨论过让React失败快速进行道具验证失败(见下面的问题),看起来它最终会发生,但我认为它还没有出现在任何路线图上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.