How can I ensure a reactjs state is updated, and then call a function?

JZ.

I'm trying to use reactjs to update a state, and once it is updated fire an ajax call requesting a new page. Just before the ajax call fires an offset variable is set: var offset = this.state.npp * this.state.page; However I find after clickNextPage() is fired, the value of this.state.page is not updated.

I fundamentally do not understand what is happening here, this appears to be a race condition, because I watch the state change on my render() function with {this.state.page}.

How can I ensure my this.state.page is updated, and then fire findByName()?

  clickNextPage: function(event){
    console.log("clicked happend")
    page = this.state.page;
    console.log(page)
    page += 1
    console.log(page)
    this.setState({page: page});
    console.log(this.state.page)
    this.findByName()
  },

JS Console:

clicked happend
0
1
0
Ed Ballot

setState is asynchronous in that this.state will not be updated right away. The short answer to your quandary is use the callback (the second parameter to setState) which is invoked after the internal state is been updated. For example

this.setState({page: page}, function stateUpdateComplete() {
    console.log(this.state.page)
    this.findByName();
}.bind(this));

The longer answer is that after you call setState, three functions are called (see here for more details about each https://facebook.github.io/react/docs/component-specs.html):

  • shouldComponentUpdate this allows you to inspect the previous and new state to determine whether the component should update itself. If you return false, the following functions are not executed (although the this.state will still be updated within your component)
  • componentWillUpdate this gives you a chance to run any code before the new state is set internally and rendering happens
  • render this happens between the component "will" and "did" functions.
  • componentDidUpdate this gives you a chance to run any code after the new state is set and the component has re-rendered itself

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I access a hover state in reactjs?

From Dev

How can I call a jQuery function after my model data has been updated?

From Dev

How can I call function?

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that my function is being called and running

From Dev

How can I call a function inside of a function?

From Dev

State is not getting updated in Reactjs

From Dev

How can I call function with different combinations

From Dev

How can I call the function of Report in VBA?

From Dev

How can I call a function within a website?

From Dev

How can I call a function inside a class?

From Dev

How can I call function in PHP

From Dev

How can I apply a function call to an alias?

From Dev

How can I call a function in a php class?

From Dev

How Can I Call a Function Properly?

From Dev

How can I succesfully call the execv function?

From Dev

How can I call function with parameter in bash?

From Dev

How can I call a function in an angular controller?

From Dev

How can I call this function in haskell?

From Dev

How can i call a javascript function

From Dev

How do I update the state (using ReactJS) if I should not call setState in componentWillUpdate?

From Dev

useEffect() chain function call not getting the updated redux state

From Dev

ReactJS - state not updated or has not effect?

From Dev

How can I ensure this CSS transition animates

From Dev

How can I ensure that illegal behavior is unexecutable?

From Dev

How can I ensure graceful scaling in kubernetes?

From Dev

How can I call the class slibing function in javascript anonymous function

From Dev

How can I call AngularJS function in JS function

Related Related

  1. 1

    How can I access a hover state in reactjs?

  2. 2

    How can I call a jQuery function after my model data has been updated?

  3. 3

    How can I call function?

  4. 4

    How can I ensure that correct function is called in case there are multiple candidates

  5. 5

    How can I ensure that correct function is called in case there are multiple candidates

  6. 6

    How can I ensure that my function is being called and running

  7. 7

    How can I call a function inside of a function?

  8. 8

    State is not getting updated in Reactjs

  9. 9

    How can I call function with different combinations

  10. 10

    How can I call the function of Report in VBA?

  11. 11

    How can I call a function within a website?

  12. 12

    How can I call a function inside a class?

  13. 13

    How can I call function in PHP

  14. 14

    How can I apply a function call to an alias?

  15. 15

    How can I call a function in a php class?

  16. 16

    How Can I Call a Function Properly?

  17. 17

    How can I succesfully call the execv function?

  18. 18

    How can I call function with parameter in bash?

  19. 19

    How can I call a function in an angular controller?

  20. 20

    How can I call this function in haskell?

  21. 21

    How can i call a javascript function

  22. 22

    How do I update the state (using ReactJS) if I should not call setState in componentWillUpdate?

  23. 23

    useEffect() chain function call not getting the updated redux state

  24. 24

    ReactJS - state not updated or has not effect?

  25. 25

    How can I ensure this CSS transition animates

  26. 26

    How can I ensure that illegal behavior is unexecutable?

  27. 27

    How can I ensure graceful scaling in kubernetes?

  28. 28

    How can I call the class slibing function in javascript anonymous function

  29. 29

    How can I call AngularJS function in JS function

HotTag

Archive