简体   繁体   English

REACT NATIVE - this.setState 在 onChangeText 中不起作用

[英]REACT NATIVE - this.setState doesn't work inside onChangeText

I'm trying to create a conditional onPress here using a gender selection.我正在尝试使用性别选择在这里创建一个条件 onPress。 If the gender is undefined, I can't go on.如果性别未定义,我不能 go 上。 Quite simple.非常简单。 But I'm a newbie and I'm not finding the trouble here.但我是新手,我没有在这里找到麻烦。

That's the gender prop in state...那是 state 中的性别道具...

  constructor(props) {
super(props);
this.state = {
  gender: ''
};

That's the const genderSelect with these 3 gender values...这就是具有这 3 个性别值的 const genderSelect ...

render() {
const genderSelected = [
  { value: 'Feminino', label: 'Feminino' },
  { value: 'Masculino', label: 'Masculino' },
  { value: 'Outro', label: 'Outro' }
];

... } ... }

That's my Dropdown class where I call the genderSelect and try to use the setState (but it doesn't work, the 'console.log' keeps returning 'undefined').那是我的下拉菜单 class,我在其中调用了 genderSelect 并尝试使用 setState(但它不起作用,'console.log' 一直返回'undefined')。 And finally, that's my conditional onPress.最后,这是我的条件 onPress。

 return (
   <>
          <Dropdown
            label='Selecione...'
            data={genderSelect}
            baseColor={"white"}
            textColor={"white"}
            itemColor={"white"}
            pickerStyle={{
              backgroundColor: "black"
            }}
            onChangeText={(value) => {
              this.setState({ gender : value })
              console.log(this.gender)
            }}
          />
            <TouchableOpacity
              style={styles.buttonContainer}
              onPress= {()=>{
                if (this.gender != undefined ) {
                  console.log(this.gender)
                  this.props.navigation.navigate("Abas")
                }
                else {
                  console.log(this.gender)
                  Alert.alert('Ops!','Favor preencher sua idade e gênero antes!');
                }
              }}
            >
              <Text style={styles.buttonText}>CONFIRMAR</Text>
            </TouchableOpacity>

Btw, I suppose the problem ocurrs inside the onChangeText / setState section.顺便说一句,我想问题出现在 onChangeText / setState 部分。 No matter how I use 'if / else' afterwards, it keeps returning 'undefined'.无论我之后如何使用“if / else”,它都会不断返回“未定义”。

It's probably a simple issue whose cause I didn't get, I know.我知道,这可能是一个简单的问题,我没有得到它的原因。 And I've searched a lot about it, but none of the answers actually helped me.我已经搜索了很多关于它的内容,但没有一个答案实际上对我有帮助。 Hope someone can help me.希望可以有人帮帮我。

Replace this.gender with this.state.genderthis.gender替换为this.state.gender

That's the error.. gender is a state variable so you have to access it like that.这就是错误.. gender是一个 state 变量,所以你必须像这样访问它。

In your return part change this在你的return部分改变这个

<TouchableOpacity
    style={styles.buttonContainer}
    onPress={() => {
      if (this.state.gender != undefined) {
        console.log(this.state.gender);
        this.props.navigation.navigate('Abas');
      } else {
        console.log(this.state.gender);
        Alert.alert('Ops!', 'Favor preencher sua idade e gênero antes!');
      }
    }}>
  <Text style={styles.buttonText}>CONFIRMAR</Text>
</TouchableOpacity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM