加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react生成组件方式

发布时间:2020-12-15 06:27:31 所属栏目:百科 来源:网络整理
导读:有三种方式,这里简单笔记,后续更新 方式一:"常量"法定义 const element = ( div h1Hello,world!/h1 h2It is {new Date().toLocaleTimeString()}./h2 /div ); 适用于简单组件,通常在xx.js文件,被其他组件调用 方式二:函数法定义 function BoilingVerdic

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

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

    const element = (
       <div>
         <h1>Hello,world!</h1>
         <h2>It is {new Date().toLocaleTimeString()}.</h2>
       </div>
     );

适用于简单组件,通常在xx.js文件,被其他组件调用

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读