gpt4 book ai didi

javascript - 渲染函数中 this.state 为 null

转载 作者:行者123 更新时间:2023-12-03 02:34:37 26 4
gpt4 key购买 nike

我正在react-native中编写一些代码,当我在渲染函数中使用this.state时,它显示它是一个空对象

import React, { Component } from "react";
import {
TextInput,
Text,
View,
AppRegistry,
Image,
StyleSheet
} from "react-native";
import Greet from "./compo/greet";

export default class App extends Component {
Constructor(props) {
Super();
this.state = {
test: "nothing to display"
};
}

render() {
return (
<View style={styles.view}>
//here is where error is coming[enter image description here][1]
<Text> {this.state.test} </Text>
<TextInput
style={styles.input}
keybordType="default"
placeholder="enter the secret "
/>
</View>
);
}
}

这显示了一个错误“类型错误 null 不是对象(评估 'this.state.test')”它只是意味着 this.state 为 null该怎么办

最佳答案

注意构造函数和 super 的字母大小写。

   constructor(props){
super(props);
this.state={
test:"nothing to display"
};
}

这应该可行,ES6 类构造函数必须调用 super(如果它们是子类)。

关于javascript - 渲染函数中 this.state 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48588841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com