【问题标题】:“How to fix ‘handlePress’ error in react-native”“如何修复 react-native 中的‘handlePress’错误”
【发布时间】:2019-04-03 18:00:57
【问题描述】:

下面是我的登录类代码:

export default class Login extends Component {

constructor(props) {
    super(props);

    this.state = {
        loggedIn: true,
    }

    handlePress = () => {
        this.props.onHomeviewPress(this.state.data);
    }
}
render() {
    return (
        <View style={styles.container}>
            <Text style={{ color: "blue", fontSize: 28, marginLeft: 10 }}> Login</Text>
            <TextInput style={styles.input}
                underlineColorAndroid="transparent"
                placeholder="Email"
                placeholderTextColor="blue"
                autoCapitalize="none"
            />

            <TextInput style={styles.input}
                underlineColorAndroid="transparent"
                placeholder="Password"
                placeholderTextColor="blue"
                autoCapitalize="none"
            />

            <Button
                onPress={() => this.handlePress.bind(this)}
                title="Login"
            />
        </View>
    );
  }
}

HANDLEPRESS 功能有问题无法正常工作并给出错误

undefined 不是对象('_this2.handlepress.bind')

请帮我解决这个问题。提前致谢

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您的 handlePress 函数在您的构造函数中定义。

    把它移到外面就可以了

    另外,您不需要绑定函数。只需onPress={this.handlePress} 即可。

    【讨论】:

    • 谢谢杰恩!它的作品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2020-11-16
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多