<!DOCTYPE html> <html lang=“en”> <head>
讯享网
讯享网<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body>
<div id="test1"></div> <div id="test2"></div> <script src="https://cdn.bootcss.com/react/16.4.0/umd/react.development.js"></script> <script src="https://cdn.bootcss.com/react-dom/16.4.0/umd/react-dom.development.js"></script> <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script> <script src="https://www.cnblogs.com/panghexin/js/prop-types.js"></script> <script type="text/babel"> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1、定义组件:方式1</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Person(props) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ( </span><ul> <li>name:{props.name}</li> <li>age:{props.age}</li> <li>sex:{props.sex}</li> </ul> )

讯享网} </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">方式2:使用类方式定义组件(运行时请将其中一个方式注释)</span> class Person extends React.Component {
render() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ( </span><ul> <li>name:{<span style="color: rgba(0, 0, 255, 1)">this</span>.props.name}</li> <li>age:{<span style="color: rgba(0, 0, 255, 1)">this</span>.props.age}</li> <li>sex:{<span style="color: rgba(0, 0, 255, 1)">this</span>.props.sex}</li> </ul> )
讯享网 } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">指定默认值</span> Person.defaultProps =<span style="color: rgba(0, 0, 0, 1)"> { name:</span>'imaginecode'<span style="color: rgba(0, 0, 0, 1)">, age: </span>17<span style="color: rgba(0, 0, 0, 1)">, sex:</span>'woman'<span style="color: rgba(0, 0, 0, 1)"> } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">指定属性值的类型和必要性--使用prop-types.js库</span> Person.propTypes =<span style="color: rgba(0, 0, 0, 1)"> { name:PropTypes.string.isRequired, age:PropTypes.number.isRequired, } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2、渲染组件标签</span> const p1 =<span style="color: rgba(0, 0, 0, 1)"> { name:</span>'imaginecode'<span style="color: rgba(0, 0, 0, 1)">, age: </span>19<span style="color: rgba(0, 0, 0, 1)">, sex:</span>'man'<span style="color: rgba(0, 0, 0, 1)"> } ReactDOM.render(</span><Person name={p1.name} age={p1.age} sex={p1.sex} />,document.getElementById('test1')); <span style="color: rgba(0, 0, 0, 1)"> const p2 </span>=<span style="color: rgba(0, 0, 0, 1)"> { name:</span>'imaginecode2'<span style="color: rgba(0, 0, 0, 1)"> } ReactDOM.render(</span><Person name={p2.name}/>,document.getElementById('test2')); </script> </body> </html>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/140352.html