reactjs - React Native Firebase 状态未定义
问题描述
我已经尝试了多种实现,但我似乎无法让状态不是未定义的。我试过箭头函数,我试过在 onChange 事件中绑定它,但似乎没有任何效果。有人可以拿我的代码库给我一些关于我做错了什么的见解吗?谢谢!
import React from 'react';
import {View, Image, StyleSheet} from 'react-native';
import {Button, Text, Input} from 'react-native-elements';
import Icon from 'react-native-vector-icons/FontAwesome';
const styles = StyleSheet.create({
heading: {
padding: 20,
},
});
export default class ContactForm extends React.Component
{
constructor(props)
{
super(props);
this.state = {
name: '',
email: '',
}
}
handleName(e)
{
this.setState({name: e.target.value})
console.log(e.target.value);
}
handleEmail(e)
{
}
submit(e)
{
console.log(this.state.name + " " + this.state.email);
}
render()
{
return (
<View style={styles.heading}>
<Text h5>Sign up to receive updates/newsletters!</Text>
<Input placeholder="your name" onChange={this.handleName.bind(this)} />
<Input placeholder="your email" onChange={this.handleEmail.bind(this)} />
<Button title="Submit" onPress={this.submit}/>
</View>
)
}
}
解决方案
嗨,您可以这样做,无需将箭头功能与“this”绑定。
export default class ContactForm extends React.Component
{
constructor(props)
{
super(props);
this.state = {
name: '',
email: '',
}
}
handleName=(e)=>
{ console.log(e);
this.setState({name: e})
}
handleEmail=(e)=>
{
this.setState({email: e})
}
submit=()=>
{
console.log(this.state.name + " " + this.state.email);
}
render()
{
return (
<View style={styles.heading}>
<Text h5>Sign up to receive updates/newsletters!</Text>
<Input placeholder="your name" onChangeText ={this.handleName} />
//change onChange to onChangeText
<Input placeholder="your email" onChangeText={this.handleEmail} />
<Button title="Submit" onPress={this.submit}/>
</View>
)
}
}
推荐阅读
- postgresql - PostgreSQL 范围查询索引
- c++ - 当使用比格式支持的精度更高的精度来显示数字时,会写出哪些数据?
- python - pandas.errors.ParserError:错误可能是由于使用多字符分隔符时忽略引号引起的
- path - 使用 Gremlin API cosmosDB 返回简单路径中顶点的特定属性
- java - Java - 如何比较番石榴范围?
- javascript - 从 response.write(string) 获取 'undefined' 是 JS
- apache-spark - Spark Structured Streaming Kafka Writer 是否支持将数据写入特定分区?
- java - 如何打印出从比 x 小 1 到 0 的一系列数字?
- xamarin - 如何允许网站在 Xamarin.Forms WebView 中保存和读取 cookie 数据?
- sql - MSSQL - 按另一列的 MAX 日期获取组的主键