How can I check the text size of an React-Native input?

Asked

Viewed 1,455 times

0

hello, I need to take the text of an input in React-Turn on and check if this typed text has less q 11 characters, how could I make that check? I’m new to React

1 answer

1


(1) Where is the text stored? In the state of a component? If applicable, just check this.state.seuTexto.length.

(2) When do you need to check? In the way you type or when you click a button?

(2.1) Pro first, you can use onChangeText in a Textinput as follows:

onChangeText={text => {
  if (text.length < 11) {
    // tem menos de onze
  }
  // atualiza o estado
  this.setState({ seuTexto: text });
}}

(2.2) To check after clicking a button, just check onPress

onPress={() => {
  if (this.state.seuTexto.length < 11) {
    // menos de 11
  }
}}

Note: (I advise to extract the functions out of the render)

  • thanks, although I’ve already managed. It was exactly that msm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.