M
M
mosikus2019-11-07 22:50:15
React
mosikus, 2019-11-07 22:50:15

How to decrease the number of characters in the input by 1 when the button is clicked?

Hello. There is a code . Tell me how to erase one character in the input'e when you click on the remove button?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
camelCaseVlad, 2019-11-07
@mosikus

You can try the following:

removeLast = () => {
    this.setState({ value: this.state.value.substring(0, this.state.value.length - 1)})
  };

and pass to the last button<button onClick={this.removeLast}>remove</button>

D
Dmitry Karpov, 2019-11-07
@dimonos33ru

The previous State must be changed via a callback

deleted = () => {
    this.setState(({value}) => {
      const str = value.slice(0, -1);
      return {
        value: str
      }
    });
  };

button onClick={this.deleted}>remove</button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question