【React】8 props的children、校验、默认值
- 1. props.children 属性
- 2. props 校验
- 3. props默认值
1. props.children 属性
- children 属性:表示
组件标签的子节点。
当组件标签有子节点时,props就会有该属性。 - chidren 属性与普通的props一样,值可以是
任意值(文本、React元素、组件,甚至是函数)
demo:
import React from 'react'
import ReactDOM from 'react-dom/client'
class User extends React.Component {render() {return (<Node1>{() &#61;> console.log("函数1")}</Node1>)}
}
const Node1 &#61; (props) &#61;> {props.children()return (<div>node1--</div>)
}const root &#61; ReactDOM.createRoot(document.getElementById("root"))
root.render(<User />)
效果&#xff1a;
2. props 校验
使用步骡
- 安装包 prop-types ( yarn add prop-types /
npm i prop-types
) - 导入 prop-types包&#xff0c;
import PropTypes from &#39;prop-types&#39;
&#xff1b; - 使用
组件名.propTypes &#61;{}
来给组件的props添加校验规则 - 按验规见面过 PropTypes象来指定
import React from &#39;react&#39;
import ReactDOM from &#39;react-dom/client&#39;
import PropTypes from &#39;prop-types&#39;
const Node1 &#61; (props) &#61;> {console.log(props)return (<div>node1--{props.colors[0]}</div>)
}
Node1.propTypes &#61; {colors: PropTypes.array
}const root &#61; ReactDOM.createRoot(document.getElementById("root"))
root.render(<Node1 colors&#61;{"qq"} />)}
效果&#xff1a;
传入错误参数时&#xff0c;会提示类型错误
常用验证规则&#xff1a;
若是必填则后面加.isRequired
MyComponent.propTypes &#61; {optionalArray: PropTypes.array,optionalBool: PropTypes.bool,optionalFunc: PropTypes.func,optionalNumber: PropTypes.number,optionalObject: PropTypes.object,optionalString: PropTypes.string,optionalSymbol: PropTypes.symbol,
optionalNode: PropTypes.node,optionalElement: PropTypes.element,optionalElementType: PropTypes.elementType,optionalMessage: PropTypes.instanceOf(Message),optionalEnum: PropTypes.oneOf([&#39;News&#39;, &#39;Photos&#39;]),optionalUnion: PropTypes.oneOfType([PropTypes.string,PropTypes.number,PropTypes.instanceOf(Message)]),optionalArrayOf: PropTypes.arrayOf(PropTypes.number),optionalObjectOf: PropTypes.objectOf(PropTypes.number),optionalObjectWithShape: PropTypes.shape({optionalProperty: PropTypes.string,requiredProperty: PropTypes.number.isRequired}),optionalObjectWithStrictShape: PropTypes.exact({optionalProperty: PropTypes.string,requiredProperty: PropTypes.number.isRequired}),requiredFunc: PropTypes.func.isRequired,requiredAny: PropTypes.any.isRequired,
customProp: function(props, propName, componentName) {if (!/matchme/.test(props[propName])) {return new Error(&#39;Invalid prop &#96;&#39; &#43; propName &#43; &#39;&#96; supplied to&#39; &#43;&#39; &#96;&#39; &#43; componentName &#43; &#39;&#96;. Validation failed.&#39;);}},customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {if (!/matchme/.test(propValue[key])) {return new Error(&#39;Invalid prop &#96;&#39; &#43; propFullName &#43; &#39;&#96; supplied to&#39; &#43;&#39; &#96;&#39; &#43; componentName &#43; &#39;&#96;. Validation failed.&#39;);}})
};
3. props默认值
指定 props 的默认值&#xff0c; 这个方法只有浏览器编译以后才会生效
class HandsomeBoy extends Component{static defaultProps &#61; {name:&#39;pyq&#39;}constructor(props){super(props)}render(){return <section>{this.props.name}</section>}
}
指定 props 的默认值&#xff0c;这个方法会一直生效
class Age extends Component{render(){return <section>{this.props.name}</section>}
}
Age.defaultProps &#61; {name:18
}