-
html标签可以在标签上写自定义属性,那么react的组件,也可以像传属性一项,给组件传props;
react组件接收到传入的属性后,会自动塞进实例的props属性中,通过this.props可以拿到外部传入的属性
来看一下props的基本使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>组件实例核心属性1:state</title> </head> <body> <!-- 准备一个容器 --> <div id="test"></div> <div id="test2"></div> <div id="test3"></div> <!-- 引入react核心库 --> <script src="https://cdn.bootcdn.net/ajax/libs/react/18.2.0/umd/react.development.min.js"></script> <!-- 引入react-dom, 用于支持react操作dom, 需要在核心库之后引入 --> <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/18.2.0/umd/react-dom.development.min.js"></script> <!-- 引入babel.js,用于将jsx转化为js --> <script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.18.7/babel.min.js"></script> <script type="text/babel"> // 1.创建类组件 class Person extends React.Component{ render() { console.log(this); const { name, age, sex } = this.props; return ( <ul> <li>姓名: {name}</li> <li>性别: {sex}</li> <li>年龄: {age}</li> </ul> ) } } // 挂载 ReactDOM.render(<Person name="tom" age="18" sex="女" />, document.getElementById('test')) ReactDOM.render(<Person name="老刘" age="30" sex="男" />, document.getElementById('test2')) ReactDOM.render(<Person name="老王" age="19" sex="女" />, document.getElementById('test3')) </script> </body> </html>
-
标签:render,react,props,组件,document,三大,属性 From: https://www.cnblogs.com/fqh123/p/16614508.html