在React Native中onPress和onChangeText什么都不做

时间:2018-12-25 09:33:07

标签: javascript reactjs react-native

我的onPressonChangeText函数什么都不做。如果输入值,则会收到此错误消息

_this.setState is not a function. (In '_this.setState({
          username: username
        })', '_this.setState' is undefined)

app / index.js

export default class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      username: "", 
      password: ""
    };

    this._handlePress = this._handlePress.bind(this)
  }

  _handlePress = () => {
     const { username } = this.state;
     const { password } = this.state;

     Alert.alert(username);
     Alert.alert(password);
     onSignIn().then(() => navigation.navigate("SignedIn")); //also not working
  }

  /**/

  render() {
    /**/
  }
}

app / screens / SignIn.js

import React from "react";

export default ({navigation, _handlePress}) => (
  <View style={{ paddingVertical: 20 }}>
    <Card title="SIGN IN">
      <FormLabel>Email</FormLabel>
      <FormInput 
        placeholder="Email address..." 
        onChangeText={username => this.setState({username})}
      />
      <FormLabel>Password</FormLabel>
      <FormInput 
        secureTextEntry 
        placeholder="Password..." 
        onChangeText={password => this.setState({password})}
      />

      <Button
        buttonStyle={{ marginTop: 20 }}
        backgroundColor="#03A9F4"
        title="SIGN IN"
        onPress={this._handlePress}
      />
    </Card>
  </View>
);

参考:https://github.com/datomnurdin/auth-reactnative

2 个答案:

答案 0 :(得分:0)

您收到此错误,因为SignIn.js js只是一个函数,而不是Component类。因此函数的“ this”没有任何“ setstate”方法。您需要在像这样的react类中编写组件

class Signin extends React.Component{

  <View style={{ paddingVertical: 20 }}>
      <Card title="SIGN IN">
        <FormLabel>Email</FormLabel>
        <FormInput 
          placeholder="Email address..." 
          onChangeText={username => this.setState({username})}
        />
        <FormLabel>Password</FormLabel>
        <FormInput 
          secureTextEntry 
          placeholder="Password..." 
          onChangeText={password => this.setState({password})}
        />

        <Button
          buttonStyle={{ marginTop: 20 }}
          backgroundColor="#03A9F4"
          title="SIGN IN"
          onPress={this._handlePress}
        />
      </Card>
    </View>
}

答案 1 :(得分:0)

G,H