javascript - 在 setState 更新状态之前调用渲染

标签 javascript reactjs setstate asynchronous-javascript

有什么方法可以确保在 setState 实际更新状态之后调用 render 吗?据我所知,setState 是异步的。我尝试将 render 作为 setState 的回调,但它仍然在状态更新之前呈现。

HTML 代码:

<div id="root"></div>

react 代码:

class Table extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            squares: []
        };
        this.create = this.create.bind(this);
    }

    create() {
        console.log('squares before: ' + this.state.squares);
        var a = [];
        for (var i = 0; i < 16; i++) {
            a.push(undefined);
        }
        i = Math.floor(Math.random() * 8);
        a[i] = 2;
        i = Math.floor(Math.random() * 8) + 8;
        a[i] = 2;
        console.log('new squares: ' + squares);
        console.log('non-updated squares: ' + this.state.squares);
        this.setState({
            squares: a
        }, console.log('updated squares: ' + this.state.squares);
    }

    componentWillMount() {
        this.create();
    }

    render() {
        console.log('rendering');
        return (<div>
                <table className="table">
                    <tbody>
                        <tr>
                            <td className="td"><Number number={this.state.squares[0]}/></td>
                            <td className="td"><Number number={this.state.squares[1]}/></td>
                            <td className="td"><Number number={this.state.squares[2]}/></td>
                            <td className="td"><Number number={this.state.squares[3]}/></td>
                        </tr>
                        <tr>
                            <td className="td"><Number number={this.state.squares[4]}/></td>
                            <td className="td"><Number number={this.state.squares[5]}/></td>
                            <td className="td"><Number number={this.state.squares[6]}/></td>
                            <td className="td"><Number number={this.state.squares[7]}/></td>
                        </tr>
                        <tr>
                            <td className="td"><Number number={this.state.squares[8]}/></td>
                            <td className="td"><Number number={this.state.squares[9]}/></td>
                            <td className="td"><Number number={this.state.squares[10]}/></td>
                            <td className="td"><Number number={this.state.squares[11]}/></td>
                        </tr>
                        <tr>
                            <td className="td"><Number number={this.state.squares[12]}/></td>
                            <td className="td"><Number number={this.state.squares[13]}/></td>
                            <td className="td"><Number number={this.state.squares[14]}/></td>
                            <td className="td"><Number number={this.state.squares[15]}/></td>
                        </tr>
                    </tbody>
                </table>
            </div>);
    }
}

class Game extends React.Component {
    newGame() {
        this.refs.table.create();
    }

    render() {
        return (<div>
            <Table ref="table"/>
            <button id="newGame" onClick={this.newGame.bind(this)}>New Game</button>
        </div>);
    }
}

基本上,这里发生的情况是首先记录“渲染”,然后记录“更新的方 block :...”,如下所示:

squares before: [undefined, 2, undefined, undefined, undefined, 2]
new squares: [undefined, undefined, 2, undefined, 2, undefined]
non-updated squares: [undefined, 2, undefined, undefined, undefined, 2]
rendering
updated squares: [undefined, undefined, 2, undefined, 2, undefined]

我尝试对 setState render 进行回调,但第二次渲染将在第一次渲染之后、setState 更新之前渲染状态:

this.setState({
  squares: a
}, this.render);

已记录:

squares before: [undefined, 2, undefined, undefined, undefined, 2]
new squares: [undefined, undefined, 2, undefined, 2, undefined]
non-updated squares: [undefined, 2, undefined, undefined, undefined, 2]
rendering
rendering

任何帮助将不胜感激。提前致谢!

最佳答案

componentWillMount 中的同步 setState 不会触发重新渲染,而是反射(reflect)初始渲染本身的变化。回调然而 setState 是在 render 方法之后调用的。

如果您在渲染方法中记录状态方 block ,它将在初始渲染本身中记录正确的结果。

P.S. componentWillMount 在最新版本中已被弃用,最好将当前逻辑放在构造函数中,而不是 componentWillMount

Working demo

关于javascript - 在 setState 更新状态之前调用渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155413/

相关文章:

react-hooks - React Form UI 未正确反射(reflect)状态变化

javascript - setState 的真实世界用法与更新回调而不是在 React JS 中传递对象

javascript - 如何使用 javascript 检索不支持的样式

javascript - 后增量与前增量 - Javascript 优化

javascript - 我们如何使用 jquery 在 url 中传递 id?

css - 如何在 Material-UI 中使图像稍微偏离对话框

javascript - 如何在文本字段中保留文本

javascript - 如何在 jwplayer 中的播放列表的最后一个视频上禁用下一个卡

javascript - 从字符串创建 react 组件

javascript - React - 如何将返回的数据从导出函数传递给组件?