[英]Cannot read property string of null in react native
I always get this error 我总是得到这个错误
Cannot read property 'todoText' of null in react native
for submitting a form. 提交表格。 I'm trying to create a form that will submit the value. 我正在尝试创建一个提交值的表单。
import * as React from "react";
import { View, Text, StyleSheet, TextInput, TouchableOpacity } from 'react-native';
interface TodoState{
todoText?: string;
}
interface TodoProps{
text: string;
}
export default class AddTodo extends React.Component <TodoProps,TodoState> {
constructor(props:TodoProps){
super(props);
this.setState({todoText:" "});
}
handleSubmit = () => {
console.log(this.state.todoText);
}
updateText = (text) => {
this.setState((state) => {
return { todoText: text };
});
};
render() {
return (
<View style={{margin: 128, marginLeft: 15, marginRight:15}}>
<TextInput style={styles.input} placeholder='Todo' onChangeText={(text) => this.setState({todoText:text})} value={this.state.todoText} />
<TouchableOpacity onPress={this.handleSubmit.bind(this)}>
<Text style={styles.button}>Submit</Text>
</TouchableOpacity>
</View>
)
}
}
I trying to follow this link but I always get that error. 我试图按照这个链接,但我总是得到那个错误。 As what I have understand in react, it work the way i bind the function. 正如我在反应中所理解的那样,它以我绑定函数的方式工作。 I don't know if react-native has another way on doing this. 我不知道react-native是否有另一种方法可以做到这一点。 I follow already the doc of it. 我已经按照它的文件。 Did I miss something about my code? 我错过了我的代码吗? But if you have tutorial that will do a form validation. 但是,如果您有可以进行表单验证的教程。 That will be a great to study deeply in react native. 对于本土的反应,这将是一个很好的研究。 I'm new on this technology. 我是这项技术的新手。
It's about your first render, it can't find todoText
. 这是关于你的第一个渲染,它找不到todoText
。 Because it's initial in the constructor
is wrong. 因为它在constructor
的初始化是错误的。 Change the constructor
to this: 将constructor
更改为:
constructor(props:TodoProps){
super(props);
this.state = {
todoText:" "
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.