react生成组件方式

前端之家收集整理的这篇文章主要介绍了react生成组件方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有三种方式,这里简单笔记,后续更新

@H_404_2@
  • 方式一:"常量"法定义

    const element = (
       <div>
         <h1>Hello,world!</h1>
         <h2>It is {new Date().toLocaleTimeString()}.</h2>
       </div>
     );
  • 适用于简单组件,通常在xx.js文件,被其他组件调用

    @H_404_2@
  • 方式二:函数法定义
  • function BoilingVerdict(props) {
      if (props.celsius >= 100) {
        return <p>水会烧开</p>;
      }
      return <p>水不会烧开</p>;
    }

    适用于简单组件,通常在xx.js文件,被其他组件调用。(这里也可以用es6定义)

    @H_404_2@
  • 方式三:ES6 class 类来定义
  • class Welcome extends React.Component {
      constructor(props) {
        super(props);
        this.state = {};
      }
      render() {
        return <h1>Hello,{this.props.name}</h1>;
      }
    }

    适用于组件里有复杂逻辑,我把他称做应用组件

    猜你在找的React相关文章