[英]PropType validations for multiple react components in a single file
I have a file which accommodates more than a single class component. 我有一个文件,可以容纳多个类。 In this example, I have a class component that used two stateless components and I would like to use it in a class component.
在这个例子中,我有一个使用两个无状态组件的类组件,我想在一个类组件中使用它。 How can I validate the props for different components.
如何验证不同组件的道具。
const Image = ({className, img}) => {
<code>
}
const MessageDetail = ({className, message}) => {
<code>
}
class Container extends Component {
constructor(props) {
super(props);
this.state = {
expandall: true,
touched: null,
};
<code>
<Image className='block-image' img='component1' />
<MessageDetail className='block-image' message='component2' />
Container.propTypes = {
model: PropTypes.object,
content: PropTypes.object,
emit: PropTypes.func,
screensize: PropTypes.string,
};
Container.defaultProps = {
model: null,
content: null,
emit: null,
screensize: 'desktop',
};
You just add it at the end of the file eg: 您只需将其添加到文件末尾,例如:
MessageDetail.propTypes = {
className: PropTypes.object,
message: PropTypes.object
};
Image.propTypes = {
className:PropTypes.object,
img: PropTypes.object
};
Nothing special is really required. 确实没有什么特别的要求。
You can code like this:- 您可以这样编写:
const Image = ({className, img}) => {
<code>
}
const MessageDetail = ({className, message}) => {
<code>
}
class Container extends Component {
constructor(props) {
super(props);
this.state = {
expandall: true,
touched: null,
};
<code>
<Image className='block-image' img='component1' />
<MessageDetail className='block-image' message='component2' />
Container.propTypes = {
model: react.PropTypes.object,
content: react.PropTypes.object,
emit: react.PropTypes.func,
screensize: react.PropTypes.string,
};
Container.defaultProps = {
model: null,
content: null,
emit: null,
screensize: 'desktop',
};
Image.propTypes = {
propA: react.PropTypes.object,
propB: react.PropTypes.object,
};
MessageDetail.propTypes = {
propC: react.PropTypes.object,
propD: react.PropTypes.object,
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.