组件的用法
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script type="text/babel">
//定义组件-函数写法
function Component1() {
return <h1>I am Component1.</h1>;
}
//定义组件-ES6写法
class Component2 extends React.Component {
render() {
return <h1>I am Component2.</h1>;
}
}
//组件数组
const element = [<Component1 /> , <Component2 />];
ReactDOM.render(
element,
document.getElementById('content')
);
</script>
<div id="content"></div>
复合组件
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script type="text/babel">
function Name(props) {
return <h1>名称:{props.name}</h1>;
}
class Sex extends React.Component {
render() {
return <h1>性别:{this.props.sex}</h1>;
}
}
//复合新组件
function Person() {
return (
<div>
<Name name="小明" />
<Sex sex="男" />
</div>
);
}
const element = <Person />;
ReactDOM.render(
element,
document.getElementById('content')
);
</script>
<div id="content"></div>