02 react简单组件

react简单组件

本案例中的组件均被拉出成为单独的文件

项目路径如下

在这里插入图片描述

组件的写法

注意:

  1. 组件名一定要大写
  2. 组件必须有 return 内容 或者return null
// ./components/com.js

import React from "react";

// 函数组件
export function Hello() {
  return <h1>这是我的一个函数组件</h1>;
}

// 组件不返回 内容 必须返回一个null
export function RetNo() {
  return null;
}

// 类组件
export class HelloRceat extends React.Component {
  render() {
    return <h1>class 组件</h1>;
  }
}

组件的使用

// ./index.js

import React from "react";
import ReactDOM from "react-dom";
import { HelloRceat, RetNo, Hello } from "./components/com.js";


const div = (
  <div>
   <HelloRceat />
        <hr>
   <RetNo />
        <hr>
   <Hello />
  </div>
);
ReactDOM.render(div, document.querySelector("#root"));

事件处理

我呢只举一个例子

// ./components/event.js


import React from "react";
// 类组件
export class Events1 extends React.Component {
  handleClick(e) {
    console.log("点击我干嘛", e);
  }
  render() {
    return <button onClick={this.handleClick}>点击</button>;
  }
}

// 函数组件
export function Events2() {
  function handleClick(e) {
    console.log("点击我干嘛", e);
  }
  return <button onClick={handleClick}>点击</button>;
}

export function Events3() {
  function handleClick(e) {
    console.log("点击我干嘛", e);
    // 阻止默认行为
    e.preventDefault();

  }
  return <a href="http://www.baidu.com" onClick={handleClick}>点击</a>;
}

页面使用

// ./index.js

import React from "react";
import ReactDOM from "react-dom";
import { Events1, Events2, Events3 } from "./components/event.js";


const div = (
  <div>
   <Events1 />
        <hr>
   <Events2 />
            <hr>
   <Events3 />
  </div>
);
ReactDOM.render(div, document.querySelector("#root"));

更新视图

注意:

  1. 只有类组件可以实现更新视图(函数组件不可以)
  2. 这之间有一个this的指向问题大家要注意
// ./components/state.js

export class StateClass extends React.Component {
  // 第一种初始data
  constructor() {
    super(); // 必须写
    this.state = {
      count: 0,
    };
    // this.handerFunc = this.handerFunc.bind(this);
  }

  // 第二种初始 data (二者取其一)
  // state = {
  //   count: 0,
  // };

  // handerFunc() { // 有this的指向问题
  //   // this.state.count++;
  //   this.setState({ count: this.state.count + 1 });
  // }
  handerFunc = () => {
    this.setState({ count: this.state.count + 1 });
  };
  render() {
    return (
      <div>
        <h2>{this.state.count}</h2>
        {/* <button onClick={() => this.setState({ count: this.state.count + 1 })}> */}
        <button onClick={this.handerFunc}>点击count++</button>
      </div>
    );
  }
}

页面使用

// ./index.js

import React from "react";
import ReactDOM from "react-dom";
import { StateClass } from "./components/state";


const div = (
  <div>
   <StateClass />
        <hr>
  </div>
);
ReactDOM.render(div, document.querySelector("#root"));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厚渡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值