【发布时间】:2020-01-21 03:47:36
【问题描述】:
React Native 新手,正在尝试应用创意。基本上我只是想创建一个当我按下按钮时出现的 TextInput 元素。下面是我的尝试。我正在尝试利用课堂上的状态,但有些东西不对劲。
Expo 抛出“null 不是对象(评估 'this.state.isShowingText')”错误。
有什么想法吗?
import React, { Component } from 'react';
import { TextInput, Alert, Button, ScrollView, Text, View, StyleSheet } from 'react-native';
export default class CoolComponent extends Component {
render() {
const nameAdd = () =>{
state = { isShowingText: true };
}
return (
<View style={{ alignItems: 'center', top: 50 }}>
<Title>Some Title</Title>
{this.state.isShowingText ? <TextInput></TextInput> : null}
<ScrollView></ScrollView>
<Button
title="Press me"
onPress={nameAdd}
/>
</View>
);
}
}
【问题讨论】:
标签: react-native button state textinput