Reactjs (反应本机)Null不是对象
这是React Native在呈现时遇到问题的代码部分:Reactjs (反应本机)Null不是对象,reactjs,react-native,Reactjs,React Native,这是React Native在呈现时遇到问题的代码部分: You input {this.state.zip}. 我是一个初学者,我正在学习“Learning React National”教程,但书中的代码不起作用 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, TextInput, Image, View } from 'react-native';
You input {this.state.zip}.
我是一个初学者,我正在学习“Learning React National”教程,但书中的代码不起作用
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Image,
View
} from 'react-native';
class WeatherProject extends Component {
// If you want to have a default zip code, you could add one here
getInitialState() {
return ({
zip: ''
});
}
// We'll pass this callback to the <TextInput>
_handleTextChange(event) {
// log statements are viewable in Xcode,
// or the Chrome debug tools
console.log(event.nativeEvent.text);
this.setState({
zip: event.nativeEvent.text
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
You input {this.state.zip}.
</Text>
<TextInput
style={styles.input}
onSubmitEditing={this._handleTextChange}/>
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
borderWidth: 2,
height: 40
}
});
AppRegistry.registerComponent('WeatherProject', () => WeatherProject);
[enter image description here][1]
import React,{Component}来自'React';
进口{
评估学,
样式表,
文本,
文本输入,
形象,,
看法
}从“反应本机”;
类WeatherProject扩展组件{
//如果你想有一个默认的邮政编码,你可以在这里添加一个
getInitialState(){
返回({
zip:'
});
}
//我们将此回调传递给
_handleTextChange(事件){
//日志语句可以在Xcode中查看,
//或者Chrome调试工具
log(event.nativeEvent.text);
这是我的国家({
zip:event.nativeEvent.text
});
}
render(){
返回(
输入{this.state.zip}。
);
}
}
var styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“居中”,
背景颜色:“#F5FCFF”,
},
欢迎:{
尺寸:20,
边界宽度:2,
身高:40
}
});
AppRegistry.registerComponent('WeatherProject',()=>WeatherProject);
[在此处输入图像描述][1]
getInitialState通常与React.createClass一起使用。要将组件定义为类,构造函数中应包含以下代码:
getInitialState() {
return ({
zip: ''
});
}
constructor() {
super();
this.state = {
zip: ''
}
}
建造商:
getInitialState() {
return ({
zip: ''
});
}
constructor() {
super();
this.state = {
zip: ''
}
}
getInitialState通常与React.createClass一起使用。要将组件定义为类,构造函数中应包含以下代码:
getInitialState() {
return ({
zip: ''
});
}
constructor() {
super();
this.state = {
zip: ''
}
}
建造商:
getInitialState() {
return ({
zip: ''
});
}
constructor() {
super();
this.state = {
zip: ''
}
}
在ES6类中(您正在扩展组件而不是使用createClass
),初始状态在构造函数中设置为this.state={zip:'}
因此,它将是
class WeatherProject extends Component {
constructor(props){
super(props);
this.state = {
zip: ""
}
}
}
在ES6类中(您正在扩展组件而不是使用createClass
),初始状态在构造函数中设置为this.state={zip:'}
因此,它将是
class WeatherProject extends Component {
constructor(props){
super(props);
this.state = {
zip: ""
}
}
}
“有麻烦”和“不工作”是什么意思?你收到了一条错误消息?发布消息。如果包含行号,请告诉我们您的帖子中的哪一行。“有问题”和“不工作”是什么意思?你收到了一条错误消息?发布消息。如果包含行号,请告诉我们您的帖子中有哪一行。