JSX render 组件 children PropTypes DOM state 表单 生命周期函数 Ajax promise

React简介:起源于Facebook内部项目,用于架设INS的网站,13年5月份开源。

1 HTML模板

首先script标签的type属性必须为text/babel,属于JSX语法,需要用到三个库,react.development.js(React的核心库) react-dom.development.jsbabel.min.js
浏览器中使用babel编译JSX效率是非常低的

2 render

每个组件都必须有的方法,用于渲染,React支持组件开发,组件之间的数据交互使用propsstateprops指不可更改的属性,state用于可以改变的数据,用setState改变值

3 生命周期函数

React中生命周期函数有三种状态
Mounting 加载
Updating 更新
Unmounting 关闭
React的七个生命周期函数
componentWillMount 组件将要被渲染 常用于使用Ajax请求时
componentDidMount 组件被渲染以后
componentWillReceiveProps 组件将要接收props和state
componentDidUpdate 组件被更新之后
componentWillUpdate 组件将要被更新
componentWillUnmount 组件将要被关闭
shouldComponentUpdate 组件是否需要被更新,返回true就更新,false就不更新

示例

// AJAX
    componentWillMount() {
        console.log("componentWillMount");
    }
    // 添加动画
    componentDidMount() {
        console.log("componentDidMount");
    }
    // 将要接收props/state数据
    componentWillReceiveProps() {
        console.log("componentWillReceiveProps");
    }
    // 更新之后
    componentDidUpdate() {
        console.log("componentDidUpdate");
    }
    // 将要更新
    componentWillUpdate() {
        console.log("componentWillUpdate");
    }
    // 将要被卸载
    componentWillUnmount() {
        console.log("componentUnmount");
    }
    shouldComponentUpdate() {
        console.log("shouldComponentUpdate");
        if (this.state.msg === "helloworld") {
            return true;
        } else {
            return false;
        }
    }

4 Ajax请求

Ajax请求会返回一个promise对象,所以必须加await

  async componentWillMount() {
    let res = await axios.get('http://localhost:8080/api/newsdata');
    let data = res.data;
    let jsondata = JSON.parse(data.forum.extra.ncov_string_list);
    let newsdata = jsondata.provinces;
    let newsdataSort = newsdata.sort((a, b) => {
      if(a.confirmedNum > b.confirmedNum) {
        return -1
      } else {
        return 1
      }
    })
    this.setState({
      newsdata: newsdataSort
    })
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值