React官网学习实践 - 事件处理

作者: 张中华 | 来源:发表于2018-11-05 22:03 被阅读5次

React 元素的事件处理和 DOM元素的很相似。但是有一点语法上的不同:

  • React事件绑定属性的命名采用驼峰式写法,而不是小写。
  • 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法)
    例如,传统的 HTML:
<button onclick="activateLasers()">
  Activate Lasers
</button>

React 中稍稍有点不同:

<button onClick={activateLasers}>
  Activate Lasers
</button>

实例:

从实例中可以看出,当修改state中的某个变量时,componentWillUpdate, componentDidUpdate也会相应的执行。
注意:
此处的函数handleClick(), 在使用类似匿名函数时(不是很清楚如何称呼)省略了参数的括号,我加上括号时,同样可以达到效果。

 handleClick() {
        this.setState((prevState) => ({
            isToggleOn: !prevState.isToggleOn
        }));
    }

你必须谨慎对待 JSX 回调函数中的 this,类的方法默认是不会绑定this 的。如果你忘记绑定 this.handleClick 并把它传入 onClick, 当你调用这个函数的时候 this 的值会是 undefined

这并不是 React 的特殊行为;它是函数如何在 JavaScript 中运行的一部分。通常情况下,如果你没有在方法后面添加 () ,例如 onClick={this.handleClick},你应该为这个方法绑定 this.

code:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {isToggleOn: true};

        // This binding is necessary to make `this` work in the callback
        this.handleClick = this.handleClick.bind(this);
    }
    componentWillMount () {
        console.log('this is in componentWillMount method.');

    }
    componentDidMount () {
        console.log('this is in componentDidMount method.');
    }
    componentWillReceiveProps (nextProps) {
        console.log('this is in componentWillReceiveProps method.');
    }
    // shouldComponentUpdate (nextProps,nextState) {
    //     console.log('this is in shouldComponentUpdate method.');
    // }
    componentWillUpdate (nextProps,nextState) {
        console.log('this is in componentWillUpdate method.');
    }
    componentDidUpdate (prevProps,prevState) {
        console.log('this is in componentDidUpdate method.');
    }
    handleClick() {
        this.setState(prevState => ({
            isToggleOn: !prevState.isToggleOn
        }));
    }

    render() {
    return (
        <div>
            <button onClick={this.handleClick}>
                {this.state.isToggleOn ? 'ON' : 'OFF'}
            </button>
        </div>
    );
  }
    componentWillUnmount () {
        console.log('this is in componentWillUnmount method.');
    }
}

export default App;

相关文章

  • React官网学习实践 - 事件处理

    React 元素的事件处理和 DOM元素的很相似。但是有一点语法上的不同: React事件绑定属性的命名采用驼峰式...

  • React

    React 《React 官网文档》 React简介 React概念 React官网学习实践 - jSX简介 Re...

  • React HOC

    在React官网文档学习React HOC,整个看了一遍还是云里雾里的,于是按照官网文档,自己动手实践一下。官网地...

  • React官网学习实践 - React安装

    创建一个新的APP 2.如果您安装了npm 5.2.0以以上版本的话,您可以使用npx作为一个更好的选择。 安装 ...

  • React官网学习实践 - 表单

    HTML表单元素与React中的其他DOM元素有所不同,因为表单元素生来就保留一些内部状态。例如,下面这个表单只接...

  • React Native 资源

    学习资源 文档 知识图谱 React Native 官网(英文) React Native 中文网 React N...

  • React学习笔记.md

    React官网学习笔记 扩展 Pete Hunt: React: Rethinking best practice...

  • React学习资源汇总

    React学习资源汇总 React 官方 官网地址:http://facebook.github.io/react...

  • React官网学习实践 - 状态提升

    使用 react 经常会遇到几个组件需要共用状态数据的情况。这种情况下,我们最好将这部分共享的状态提升至他们最近的...

  • React官网学习实践 - 条件渲染

    在 React 中,你可以创建不同的组件来封装各种你需要的行为。然后还可以根据应用的状态变化只渲染其中的一部分。 ...

网友评论

    本文标题:React官网学习实践 - 事件处理

    本文链接:https://www.haomeiwen.com/subject/oleexqtx.html