有三种方式,这里简单笔记,后续更新es6
方式一:"常量"法定义函数
const element = ( <div> <h1>Hello, world!</h1> <h2>It is {new Date().toLocaleTimeString()}.</h2> </div> );
适用于简单组件,一般在xx.js文件,被其余组件调用this
function BoilingVerdict(props) { if (props.celsius >= 100) { return <p>水会烧开</p>; } return <p>水不会烧开</p>; }
适用于简单组件,一般在xx.js文件,被其余组件调用。(这里也能够用es6定义)code
class Welcome extends React.Component { constructor(props) { super(props); this.state = {}; } render() { return <h1>Hello, {this.props.name}</h1>; } }
适用于组件里有复杂逻辑,我把他称作应用组件element