React入门(2)-setSate()

2021-02-18  本文已影响0人  申_9a33

1.State的异步更新

import React, { Component } from 'react'

export default class ClassComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            counter: 0,
        }
    }

    changevalue = v => {
        this.setState({
            counter: this.state.counter + v
        }, () => {
            console.log('counter-2', this.state.counter) // 值变换后的回调函数
        })

        console.log('counter-1', this.state.counter)

    }

    setCounter = (e) => {
        this.changevalue(1)
    }


    componentDidMount() {
        this.changevalue(1)
    }

    componentWillUnmount() {
    }

    render() {
        const { counter } = this.state;
        return (
            <div>
                <h3>setStatePage</h3>
                <button onClick={this.setCounter}>counter:{counter}</button>
            </div>
        )
    }
}

在上述代码中,counter-1 为旧值,counter-2为新值


2.state同步更新-定时器

import React, { Component } from 'react'

export default class ClassComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            counter: 0,
        }
    }

    changevalue = v => {
        setTimeout(() => {
            this.setState({
                counter: this.state.counter + v
            })
            console.log('counter', this.state.counter)
        }, 0)
    }

    setCounter = (e) => {
        this.changevalue(1)
    }


    componentDidMount() {
        this.changevalue(1)
    }

    render() {
        const { counter } = this.state;
        return (
            <div>
                <h3>setStatePage</h3>
                <button onClick={this.setCounter}>counter:{counter}</button>
            </div>
        )
    }
}


3.state同步更新-原生事件

import React, { Component } from 'react'

export default class ClassComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            counter: 0,
        }
    }

    changevalue = v => {
        this.setState({
            counter: this.state.counter + v
        })
        console.log('counter', this.state.counter)
    }

    setCounter = (e) => {
        this.changevalue(1)
    }


    componentDidMount() {
        document.body.addEventListener('click', this.setCounter, false)
    }

    render() {
        const { counter } = this.state;
        return (
            <div>
                <h3>setStatePage</h3>
                <button>counter:{counter}</button>
            </div>
        )
    }
}

4.合并更新

import React, { Component } from 'react'

export default class ClassComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            counter: 0,
        }
    }

    changevalue = v => {
        this.setState({
            counter: this.state.counter + v
        })
        console.log('counter', this.state.counter)
    }

    setCounter = (e) => {
        this.changevalue(1)
        this.changevalue(2)
    }

    render() {
        const { counter } = this.state;
        return (
            <div>
                <h3>setStatePage</h3>
                <button onClick={this.setCounter}>counter:{counter}</button>
            </div>
        )
    }
}

5.链式调用

import React, { Component } from 'react'

export default class ClassComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            counter: 0,
        }
    }

    changevalue = v => {
        this.setState(state => ({
            counter: state.counter + v
        }), () => {
            console.log('counter', this.state.counter)
        })

    }

    setCounter = (e) => {
        this.changevalue(1)
        this.changevalue(2)
    }

    render() {
        const { counter } = this.state;
        return (
            <div>
                <h3>setStatePage</h3>
                <button onClick={this.setCounter}>counter:{counter}</button>
            </div>
        )
    }
}
上一篇下一篇

猜你喜欢

热点阅读