简介 目录 评价 推荐
  • Luuuuuuuu 2023-03-31

    点击事件(两种方式)

    onClick={this.handleClick.bind(this)}

    onClick={() => {this.handleClick()}}

    http://img1.mukewang.com/64265285000109f307490574.jpg

    0赞 · 0采集
  • Luuuuuuuu 2023-03-31

    State
    this.setState 更改State中的值


    constructor

    这是ES6对类(Class)的默认方法,一般用于数据初始化,通过 new 命令生成对象实例时自动调用该方法。并且,该方法是类中必须有的,如果没有显示定义,则会默认添加。

    super

    继承它的父级

    子类必须在constructor() 中调用 super() ,否则新建实例时会报错


    0赞 · 0采集
  • 叶云逸 2022-08-09

    State(状态)

    组件内部的数据 可以动态改变

    0赞 · 0采集
  • MRNUR 2022-05-04

    在 react 中,创建某一个响应式数据可以使用在 this.state = {} 中添加属性的方法,这一点和 Vue 有明显的不同,Vue 是在 data 属性中添加响应式数据;

     class LikeBtn extends React.component { 
         constructor(props) {
         super(props)
         this.state = {
             likes: 0
         }
         }
    }


    在 react 中,添加事件和事件监听函数:

    例如:点击事件:  onclick、事件监听函数: increaseLikes

    使用驼峰命名原生 JS 事件onClick

    并在 {} 中添加 this.increaseLikes


    第一种写法:

    increaseLikes() {
        // ...
    }
    render() {
        return (
            <button onClick={this.increaseLikes}> like+ </button>
        )
    }


    第二种写法:

    increaseLikes() {
        // ...
    }
    render() {
        return (
            <button onClick={() => {this.increaseLikes()}}> like+ </button>
            { this.state.likes }
        )
    }


    上述两种写法,在 JSX 种使用箭头函数可以省略在组件 constructor  中在 this.increaseLikes 上绑定 this 的步骤;

    如果不使用箭头函数,必须要在组件 constructor  中在 this.increaseLikes 上绑定 this,否则在事件监听函数中 this undefined


    在事件监听函数中更改 state 里的数据时,需要使用 this.setState() 方法,传入对象,这里的对象就是 state 对象,用更改对象属性的方法更改 state 中的数据;

    0赞 · 0采集
  • 想成为技术大牛的程序猿 2022-02-19

    React基本结构

    http://img2.mukewang.com/6210bb310001777317181128.jpg

    0赞 · 0采集
  • powerful_girl 2021-06-01

    点赞的按钮

    0赞 · 0采集
  • moocer9527 2021-01-14

    state

    截图
    0赞 · 0采集
  • 天天57 2020-09-10

    Component中所有方法调用this的时候,都不是自动绑定的,要通过代码进行bind

    - this.increaseLikes = this.increaseLikes.bind(this)

    - onClick = {() => { this.increaseLikes() }}

    State属性是不能直接赋值更改的,改变它的唯一途径是this.setState({}),它的参数是一个object

    0赞 · 0采集
  • Foreverixhy 2020-08-16
    State的更新<pre class="brush:js;toolbar:false"><br/></pre>
    截图
    0赞 · 0采集
  • 煮酒狂歌 2020-08-06

    使用setState 改变 state中的值

    截图
    0赞 · 0采集
  • 煮酒狂歌 2020-08-06

    onClick调用方法需要手动绑定到当前类,默认this指向调用的组件;或者在调用onClick的时候使用箭头函数,不改变this指向,也可以

    截图
    0赞 · 0采集
  • 煮酒狂歌 2020-08-06

    state

    截图
    0赞 · 0采集
  • 白菜程序员 2020-03-27
    //
    this.increaseLikes = this.increaseLike.bind(this)
    //state的值不能够修改,因为其为纯函数,唯一改变state值的
    方式就是调用setState({count:++this.state.count})方法
    0赞 · 0采集
  • JacobHsu 2020-01-13

    State值是不能直接修改的 唯一修改的途徑是調用setState方法

    0赞 · 0采集
  • 爱拉舞imooc 2020-01-09

    如果不用箭头函数,则需要绑定this。

    onClick{ () => {this.increaseLikes() }}


    绑定方法1:

    onClick={this.increaseLikes.bind(this)

    绑定方法2:

    this.increaseLikes = this.increaseLikes.bind(this)

    0赞 · 0采集
  • pinzaghi 2019-07-24

    调用setState方法进行属性修改

    截图
    0赞 · 0采集
  • pinzaghi 2019-07-24

    使用bind函数将方法(this)绑定到对象上

    截图
    0赞 · 0采集
  • pinzaghi 2019-07-24

    JavaScript的类中的方法没有自动绑定this

    截图
    0赞 · 0采集
  • pinzaghi 2019-07-24

    stete是组件内部的数据状态,可以动态改变

    截图
    0赞 · 0采集
  • SongWihtinClouds 2019-04-22

    flag,点赞按钮https://img2.mukewang.com/5cbda6fb0001e5f523081468.jpg

    0赞 · 0采集
  • 慕标1545130 2019-04-16
    • 属性:不可变,外部传入

    • 状态(state):可变,私有

    组件内部的数据可以动态改变

    This.setState()是更新state的唯一途径

    例:点赞按钮


    0赞 · 0采集
  • Qun_Chen 2019-03-20

    state(状态)

    组件内部的数据可以动态改变

    this.setState()是更新state的唯一途径

    constructor

    需要显式调用super()

    方法直接在类中定义,不需要像vue中定义在methods中

    方法中的this默认指向是null,可以再构造其中进行绑定

    也可以使用箭头函数(=>)

    1赞 · 0采集
  • 人笨就要多读书 2019-02-25

    state改变

    截图
    0赞 · 0采集
  • Enli 2019-02-06

    点赞按钮的详细代码

    截图
    0赞 · 0采集
  • Enli 2019-02-06

    LikesButton代码

    截图
    0赞 · 0采集
  • Enli 2019-02-06

    State 状态

    组件私有的。

    截图
    1赞 · 0采集
数据加载中...
开始学习 免费