2

i am new to react and was making a basic counter. Logic might not be perfect but for now i want my counter to increase until the input value. Currently it decreases until 0 and increases unlimited. how can implement or bind my onChange function to onIncrease function?

import React from 'react';
import './App.css';

class App extends React.Component{
  constructor(){
    super();
    this.state = {
      counter: 0,
      inputText: '',
    }
  }

  // TODO: i should be able to increase only up to given input number
  onChange = (event) => {
    let inputText = event.target.value;
    this.setState({inputText});
  }

  onIncrease = () => {
      let counter = this.state.counter +1;
      this.setState({counter})
  }

  onDecrease = () => {
    if (this.state.counter > 0){
      let counter = this.state.counter -1;
      this.setState({counter})
    } 
  }

  render() {
    console.log(this.state.counter);
    return (
      <div className="App">
        <h2 id='s'>Counter: {this.state.counter}</h2>
        <input onChange= {this.onChange}/>
        <button onClick={this.onIncrease}>Increase</button>
        <button onClick={this.onDecrease}>Decrease</button>
      </div>
    )
  }
}

export default App;

4 Answers 4

3

In your decrease function, you check to see that the current counter is larger than zero, before decreasing. You should implement a similar check in the increase function. E.g., check that the current counter is below the value from the input field.

A couple of more things I would do to reduce the amount of logic in the increase function to a minimum:

  • In the initial state, set a "maxValue" property to Infinity
  • parse the input value an int in the onChange function, and use it to update the maxValue property of the state
  • Use the maxValue from the state in the increase function
// in constructor:
this.state = {
  ...
  maxValue: Infinity
};

// in onChange:
this.setState({
  maxValue: parseInt(e.target.value)
});

// in increase function:
if (this.state.counter < this.state.maxValue) { 
  //increase here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this helped. Yes, i was aware that my logic was somewhat wrong. Now i learned to put correct logic first and then implement it)
1

You can put a similar check as you have put in onDecrease method.

And I think one more check should be put incase the input value was more that the current counter value. May be we can change the counter to the new input value.

  onChange = event => {
    let inputText = event.target.value;
    if (this.state.counter > Number(inputText)) {
      this.setState({
        counter: Number(inputText),
        inputText
      });
    } else {
      this.setState({ inputText });
    }
  };

  onIncrease = () => {
    if (this.state.counter < Number(this.state.inputText)) {
      let counter = this.state.counter + 1;
      this.setState({ counter });
    }
  };

You can add checks to user inputs too, like if a user inputs some string.

1 Comment

You could store the limit directly in the state as a number, instead of having to do Number(this.state.inputText) everywhere.
0

You want to cap the min and max values of the count:

onChange = event => {
  let inputText = Number(event.target.value);
  this.setState({ inputText });
};

onIncrease = () => {
  let counter = Math.min(this.state.counter + 1, this.state.inputText);
  this.setState({ counter });
};

onDecrease = () => {
  let counter = Math.max(0, this.state.counter - 1);
  this.setState({ counter });
};

Edit epic-benz-fqbo6

Comments

-1
import React from 'react';
import './App.css';

class App extends React.Component{
  constructor(){
    super();
    this.state = {
      counter: 0,
      inputValue: 0,
    }
  }

  // TODO: i should be able to increase only up to given input number
  onChange = (event) => {
    let inputValue = event.target.value;
    this.setState({inputValue});
  }

  onIncrease = () => {
      if (this.state.counter < this.state.inputValue){
      let counter = this.state.counter +1;
      this.setState({counter})
  }

  onDecrease = () => {
    if (this.state.counter > 0){
      let counter = this.state.counter -1;
      this.setState({counter})
    } 
  }

  render() {
    console.log(this.state.counter);
    return (
      <div className="App">
        <h2 id='s'>Counter: {this.state.counter}</h2>
        <input onChange= {this.onChange}/>
        <button onClick={this.onIncrease}>Increase</button>
        <button onClick={this.onDecrease}>Decrease</button>
      </div>
    )
  }
}

export default App;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.