小编Kar*_*mar的帖子

对渲染进行反应以显示先前的状态值

我是ReactJS的初学者,刚开始学习并开始编写用于猜测数字的代码,但是猜测计数显示不同的值。{this.state.attempts}持有编号 用户尝试查找答案以显示正确值的尝试次数。但是{this.state.result}显示每次点击的结果,但是如果用户找到答案,它将显示以前的状态。我想知道这是怎么发生的。那是因为它不在下面render()吗?

import React, { Component } from 'react';

export default class NoLifeCycComps extends Component {
    constructor(props) {
        super(props);
        this.state = this.getInitialState();
        this.checkValue = this.checkValue.bind(this);
        this.updateInput = this.updateInput.bind(this);
    }

    randNum(){
        return Math.floor((Math.random() * 100) + 1);
    }

    getInitialState(){
        return {
            num: this.randNum(),
            inputValue: '',
            attempts: 0,
            result: '',
            reset : false
        }
    }

    reset() {
        this.setState(this.getInitialState());
    }

    checkValue() {
        this.setState((prevState) => {
            return { attempts: prevState.attempts + 1 }
        });
        if (this.state.inputValue > this.state.num) …
Run Code Online (Sandbox Code Playgroud)

render reactjs

3
推荐指数
1
解决办法
1917
查看次数

标签 统计

reactjs ×1

render ×1