Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修复react native中setState上的未定义对象_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何修复react native中setState上的未定义对象

Javascript 如何修复react native中setState上的未定义对象,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我目前是react native的新手,我想知道当我在native base的输入文本上键入时,错误显示undefined不是对象(评估e.target.value)为什么e.target.value会出错 我的目标:当按下按钮时,我输入的状态将发出警报 我将向大家展示我创建的示例代码 class WelcomeScreen extends Component { constructor(props) { super(props); this.state = {

我目前是react native的新手,我想知道当我在native base的输入文本上键入时,错误显示undefined不是对象(评估e.target.value)为什么e.target.value会出错

我的目标:当按下按钮时,我输入的状态将发出警报

我将向大家展示我创建的示例代码

    class WelcomeScreen extends Component {

  constructor(props) {
    super(props);

    this.state = {
      usernameInput:'',
      passwordInput:'',
    }

    this._onPressButton = this._onPressButton.bind(this);
    this._handleUsername = this._handleUsername.bind(this);
    this._handlePassword = this._handlePassword.bind(this);
  }


  _onPressButton() {
    alert(this.state.usernameInput);
  }

  _handleUsername = (e) => {
    this.setState({
      usernameInput:e.target.value
    })
  }

  _handlePassword = (e) =>  {
    this.setState({
      passwordInput:e.target.value
    })
  }

   render() {
     return (
      <Container>

        <View>
            <Image resizeMode="contain" style={{width: '100%', height: '40%',marginTop:'20%'}} 
              source={require('../Tracker/android/Assets/deliveryman.png')} />
        </View>

        <View style={{bottom:70,alignItems:'center',justifyContent:'center'}}>

          <Item rounded style={{width:'80%', borderRadius: 5, height:40}}>
            <Input placeholder="Username" 
            value={this.state.usernameInput} 
            onChangeText={this._handleUsername}
            style={{fontFamily: 'Gill Sans',}} />
          </Item>

          <Item rounded style={{width:'80%',marginTop:20,borderRadius: 5, height:40}}>
            <Input placeholder="Password" 
            value={this.state.passwordInput} 
            onChangeText={this._handlePassword} 
            secureTextEntry={true} 
            style={{color:'blue',fontFamily: 'Gill Sans'}}/>
          </Item>

          <LinearGradient 
            start={{x: 1, y: 0}} 
            end={{x: 0, y: 0}} 
            colors={['#2C73D2', '#008E9B']} 
            style={styles.linearGradient}>
            <TouchableOpacity onPress={this._onPressButton}>
            <Text style={styles.buttonText}>
              Login
            </Text>
            </TouchableOpacity>
          </LinearGradient>
        </View>

        <View style={{width:'100%',justifyContent:'center',alignItems:"center",marginTop:50}}>
          <Text style={{fontWeight:'300',fontFamily: 'Gill Sans',}}>© 2019 Hi-Flyer Food. </Text>
          <Text style={{fontWeight:'300',fontFamily: 'Gill Sans',}}>Designed by Solutions Experts and Enablers, Inc.</Text>
        </View>

      </Container>
     )
   }
}
class WelcomeScreen扩展组件{
建造师(道具){
超级(道具);
此.state={
用户名输入:“”,
密码输入:“”,
}
this.\u onPressButton=this.\u onPressButton.bind(this);
this.\u handleUsername=this.\u handleUsername.bind(this);
this.\u handlePassword=this.\u handlePassword.bind(this);
}
_按按钮(){
警报(this.state.usernameInput);
}
_handleUsername=(e)=>{
这是我的国家({
usernameInput:e.target.value
})
}
_handlePassword=(e)=>{
这是我的国家({
密码输入:e.target.value
})
}
render(){
返回(
登录
©2019 Hi Flyer食品。
由解决方案专家和使能器公司设计。
)
}
}

React Native不像Reactjs那样需要获取
e.target.value

onChangeText
中,函数中传递的值已经是文本值

请看下面的图片

改变这个

  _handleUsername = (e) => {
    this.setState({
      usernameInput:e.target.value // e is the text from the input
    })
  }

  _handlePassword = (e) =>  {
    this.setState({
      passwordInput:e.target.value // e is the text from the input
    })
  }


onChangeText
只传递输入的文本,所以试试这个:
\u handlePassword=(text)=>{this.setState({passwordInput:text});}
@MayankShukla也谢谢你的建议谢谢你的帮助,在我看来,Reactjs和native是一样的,这就是我再次使用e.target.value btw的原因
  _handleUsername = usernameInput => {
    this.setState({ usernameInput }) // setState with the text from the input
  }

  _handlePassword = passwordInput =>  {
    this.setState({ passwordInput }) // setState with the text from the input
  }