HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptreactCritical

Can I execute a function after setState is finished updating?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
setstateupdatingexecutefunctionfinishedaftercan

Problem

I am very new to ReactJS (as in, just started today). I don't quite understand how setState works. I am combining React and Easel JS to draw a grid based on user input. Here is my JS bin:
http://jsbin.com/zatula/edit?js,output

Here is the code:

var stage;
   
    var Grid = React.createClass({
        getInitialState: function() {
            return {
                rows: 10,
                cols: 10
            }
        },
        componentDidMount: function () {
            this.drawGrid();
        },
        drawGrid: function() {
            stage = new createjs.Stage("canvas");
            var rectangles = [];
            var rectangle;
            //Rows
            for (var x = 0; x 
                    
                        
                        Rows: { this.state.rows }
                        Columns: {this.state.cols }
                    
                    
                        
                            Number of Rows
                            
                                1
                                2
                                5
                                10
                                12
                                15
                                20
                            
                            Number of Columns
                            
                                1
                                2
                                5
                                10
                                12
                                15
                                20
                            
                        
                        
                
            );
        }
    });
    ReactDOM.render(
        ,
        document.getElementById("container")
    );


You can see in the JSbin when you change the number of rows or columns with one of the dropdowns, nothing will happen the first time. The next time you change a dropdown value, the grid will dra

Solution

setState(updater[, callback]) is an async function:

https://facebook.github.io/react/docs/react-component.html#setstate

You can execute a function after setState is finishing using the second param callback like:

this.setState({
    someState: obj
}, () => {
    this.afterSetStateFinished();
});


The same can be done with hooks in React functional component:

https://github.com/the-road-to-learn-react/use-state-with-callback#usage

Look at useStateWithCallbackLazy:

import { useStateWithCallbackLazy } from 'use-state-with-callback';

const [count, setCount] = useStateWithCallbackLazy(0);

setCount(count + 1, () => {
   afterSetCountFinished();
});

Code Snippets

this.setState({
    someState: obj
}, () => {
    this.afterSetStateFinished();
});
import { useStateWithCallbackLazy } from 'use-state-with-callback';

const [count, setCount] = useStateWithCallbackLazy(0);

setCount(count + 1, () => {
   afterSetCountFinished();
});

Context

Stack Overflow Q#34687091, score: 641

Revisions (0)

No revisions yet.