Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Field value does not change #19326

Closed
ximik753 opened this issue Jul 12, 2020 · 6 comments
Closed

Bug: Field value does not change #19326

ximik753 opened this issue Jul 12, 2020 · 6 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@ximik753
Copy link

bug

React version: 16.13.1

Link to code example: https://repl.it/@Sc_Ximik/Create-React-App

The current behavior

If I call the "update" method, through the "onClickHandler" method, my offset field does not change to 0

The expected behavior

If I call the "update" method, through the "onClickHandler" method, my offset field changes to 0

@ximik753 ximik753 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jul 12, 2020
@ximik753 ximik753 changed the title Bug: Bug: Field value does not change Jul 12, 2020
@winterlood
Copy link

winterlood commented Jul 12, 2020

The solution to this problem is to define a function in onClick, not a function call.

Instead of

onclick={onClickHandler},

Use

onclick={()=>onClickHandler()}

or if you don't use arrow funciton
Use

onclick={function(){onClickHandler()}}

@ximik753
Copy link
Author

ximik753 commented Jul 12, 2020

The solution to this problem is to define a function in onClick, not a function call.

Instead of

onclick={onClickHandler},

Use

onclick={()=>onClickHandler()}

or if you don't use arrow funciton
Use

onclick={function(){onClickHandler()}}

1
your method does not solves the problem, I still do not get 0 in the console

@winterlood
Copy link

winterlood commented Jul 12, 2020

I think it's because setState has asynchronous characteristics. The first call will be ignored.

If you have the same key to run setState, the last key delivered takes precedence.

@winterlood
Copy link

import React, { Component } from 'react';
class App extends Component {
  state = {
    count: 0
  }
  add() {
    this.setState({count: this.state.count+1});
  }
  handleClick() {
    this.add();
    this.add();
    this.add();
  }
  
  render() {
    return (
      <div>
        <p>{this.state.count}</p>
        <button onClick={() => this.handleClick()}>Add</button>
      </div>
    );
  }
}

export default App;

The intention of this code is to increase the value of this.state.count by three.
But the code above actually increases by one, not three.

This is because setState calls are made asynchronously. SetState queues changes without immediate updating of components and updates them in batches with multiple changes.

This means that if you call the setState several times, only one rendering can be done, so this.state will have only one value without changing during multiple setState operations.

@ximik753
Copy link
Author

I think it's because setState has asynchronous characteristics. The first call will be ignored.

If you have the same key to run setState, the last key delivered takes precedence.

but in my situation I need to change the "offset" field to 0, how to do it right?

@bvaughn
Copy link
Contributor

bvaughn commented Jul 12, 2020

setState is asynchronous. If you want to accumulate values in a way like this, check out the updater function form of setState as the docs show:
https://reactjs.org/docs/react-component.html#setstate

@bvaughn bvaughn closed this as completed Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants