setState的写法

setState()有以下两种写法,

  1. 对象式写法。如:setState({})或者setState({},callback)
  2. 函数式写法。如:setState((state,props) => {})或者setState((state,props) => {},callback)

计数器案例

在这里插入图片描述

新建react项目,主要涉及的文件有,

  1. 模板文件index.html
  2. 入口文件index.js
  3. 入口组件App.js
  4. components/Counter/index.jsx,即Counter组件

模板文件index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>react_app</title>
</head>
<body>
    <div id="root"></div>
</body>
</html>

入口文件index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App.js";

ReactDOM.render(<App/>,document.getElementById("root"));

入口组件App.js

import React, { Component } from 'react'
import Counter from "./components/Counter";

export default class App extends Component {
  render() {
    return (
      <div><Counter/></div>
    )
  }
}

Counter/index.jsx(Counter组件)

import React, { Component } from 'react'

export default class Counter extends Component {

    state = {
        count:0
    }
    increment = () => {
        const {count} = this.state;
        this.setState({
            count:count+1
        })
    }

    render() {
        const {count} = this.state;
        const {increment} = this;
        return (
            <div>
                <h2>当前值为:{count}</h2>
                <button onClick={increment}>点我加1</button>
            </div>
        )
    }
}

对象式写法

代码变更涉及的文件是,Counter/index.jsx,即Counter组件。
所谓对象式写法,是指setState()的第一个参数是一个对象,即setState({})
setState()也接受第二个参数,是一个回调函数,即setState({},callback)。state更新且页面渲染完成后,该回调函数才会被调用。

setState({})

import React, { Component } from 'react'

export default class Counter extends Component {
    state = {
        count:0
    }
    increment = () => {
        const {count} = this.state;
        this.setState({count:count+1})
    }

    render() {
        const {count} = this.state;
        const {increment} = this;
        return (
            <div>
                <h2>当前值为:{count}</h2>
                <button onClick={increment}>点我加1</button>
            </div>
        )
    }
}

setState({},callback)

import React, { Component } from 'react'

export default class Counter extends Component {
    state = {
        count:0
    }
    increment = () => {
        const {count} = this.state;
        this.setState({count:count+1},() => {
            console.log(this.state.count);
        })
    }

    render() {
        const {count} = this.state;
        const {increment} = this;
        return (
            <div>
                <h2>当前值为:{count}</h2>
                <button onClick={increment}>点我加1</button>
            </div>
        )
    }
}

函数式写法

代码变更涉及的文件是,Counter/index.jsx,即Counter组件。
所谓函数式写法,是指setState()的第一个参数是一个函数,即setState((state,props) => {})
setState()也接受第二个参数,是一个回调函数,即setState((state,props) => {},callback)。state更新且页面渲染完成后,该回调函数才会被调用。

setState((state,props) => {})

import React, { Component } from 'react'

export default class Counter extends Component {
    state = {
        count:0
    }
    increment = () => {
        this.setState((state,props) => {
            console.log(state,props);
            return {count:state.count+1}
        })
    }

    render() {
        const {count} = this.state;
        const {increment} = this;
        return (
            <div>
                <h2>当前值为:{count}</h2>
                <button onClick={increment}>点我加1</button>
            </div>
        )
    }
}

setState((state,props) => {},callback)

import React, { Component } from 'react'

export default class Counter extends Component {

    state = {
        count:0
    }
    increment = () => {
        this.setState((state,props) => {
            console.log(state,props);
            return {count:state.count+1}
        },() => {
            console.log(this.state.count);
        })
    }

    render() {
        const {count} = this.state;
        const {increment} = this;
        return (
            <div>
                <h2>当前值为:{count}</h2>
                <button onClick={increment}>点我加1</button>
            </div>
        )
    }
}

相关链接

【React】setState

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值