最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React.JS working with two values in state - Stack Overflow

matteradmin5PV0评论

I'm trying to do math operation from two inputs. First update state and then add two values with newly updated state.

<input type="number" onChange={this.handleIneChange.bind(this)} value={this.state.ine} />

This code isn't working: (result is always one number behind)

handleIneChange(e) {
    this.setState({ine: e.target.value});
    this.state.resultMonth = +this.state.ine - +this.state.expense;
}

This code is working:

handleIneChange(e) {
    const ine = e.target.value;
    this.state.resultMonth = +ine - +this.state.expense;
    this.setState({ine: e.target.value});
}

Not sure if I understand concept of React.JS state correctly. Definitely don't understand why first code don't work.

I'm trying to do math operation from two inputs. First update state and then add two values with newly updated state.

<input type="number" onChange={this.handleIneChange.bind(this)} value={this.state.ine} />

This code isn't working: (result is always one number behind)

handleIneChange(e) {
    this.setState({ine: e.target.value});
    this.state.resultMonth = +this.state.ine - +this.state.expense;
}

This code is working:

handleIneChange(e) {
    const ine = e.target.value;
    this.state.resultMonth = +ine - +this.state.expense;
    this.setState({ine: e.target.value});
}

Not sure if I understand concept of React.JS state correctly. Definitely don't understand why first code don't work.

Share Improve this question asked Jun 15, 2016 at 6:17 Aleš ChromecAleš Chromec 2551 gold badge4 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You shouldn't modify the state directly using this.state = (except during state initialisation). Make all your state modifications using the setState API.

For example:

handleIneChange(e) {
  const newIne = e.target.value;
  this.setState({
    ine: newIne,
    resultMonth: newIne - this.state.expense
  });
}

Update: based on the codepen and problem described by OP in ments below.

You could do something like the following to solve the issue of reusability.

handleDataChange(ine, expense) {
  this.setState({
    ine: ine,
    expense: expense,
    resultMonth: ine - expense
  });
}

handleIneChange(e) {
  const newIne = e.target.value;
  this.handleDataChange(newIne, this.state.expense);
}

handleExpenseChange(e) {
  const newExpense = e.target.value;
  this.handleDataChange(this.state.ine, newExpense);
}
Post a comment

comment list (0)

  1. No comments so far