D
D
Dmitry Kuzmenko2019-04-03 12:27:24
React Native
Dmitry Kuzmenko, 2019-04-03 12:27:24

How to change the State of a parent from a child element?

In parent

getlogin = (text) => {
      this.setState({ login: text })
   }

   state = {
      component: <Addprofile getLogin={this.getLogin} />,
      login: ''
   }

In a child element I call via props
<TextInput style = {styles.input}
               underlineColorAndroid = "transparent"
               placeholder = "Логин инстаграм"
               placeholderTextColor = "#9a73ef"
               autoCapitalize = "none"
               onChangeText = {props.getLogin(text)}
               />

Error Cannot read property 'getLogin' of undefined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-04-03
@kuzmenkodiman

1. Fix handler name:

getLogin = (text) => {
  this.setState({ login: text })
};

2. We fix the transfer of the handler to TextInput:
<TextInput style = {styles.input}
  /* ... */
  onChangeText = {this.props.getLogin}
/>

A
Alexey Ryazanov, 2019-04-03
@solargate

this.props.getLogin(text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question