小工具知识网

当前位置: 首页 > 行业知识 > React改变数据【案例】

行业知识

React改变数据【案例】

2026-11-13 05:39:43 来源:互联网转载

State��统方式

      React Demo                                  class Weather extends React.Component{        constructor(props) {      super(props)      // 初始化状态      this.state={isHot:false}      this.changeWrather = this.changeWrather.bind(this)    }    render() {      // 读取状态      const {isHot} = this.state      return this.changeWrather}今天天气很{isHot?'炎热':'凉爽'}    }    changeWrather() {      // 获取原来的isHot      const isHot = this.state.isHot      // 注意:状态必须通过setState进行更新 状态state不可直接更改      this.setState({isHot:!isHot})    }   }    // 渲染    ReactDOM.render(,document.getElementById('test'))

State简写方式(推荐)

      React Demo                                   //创建组件   class Weather extends React.Component{  // 初始化状态    state={isHot:false}    render() {      // 读取状态      const {isHot} = this.state      return this.changeWrather}今天天气很{isHot?'炎热':'凉爽'}    }    // 自定义方法    changeWrather=() =>{      // 获取原来的isHot      const isHot = this.state.isHot      // 注意:状态必须通过setState进行更新 状态state不可直接更改      this.setState({isHot:!isHot})    }   }    // 渲染    ReactDOM.render(,document.getElementById('test'))

上一篇:怎么在服务器推荐软件?

下一篇:dns服务器怎么填数据?