我今天尝试在 React Native 中编写代码,并尝试编写一个带有按钮和每次按下都会改变的文本的程序:
import { Text, View, StyleSheet, TouchableOpacity, Button, TextInput } from 'react-native';
import Constants from 'expo-constants';
export default () => {
const onButton = () =>
{
myResult(10);
}
return (
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
)
}
它给我这个错误:
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
如果我这样评论文本类或按钮类:
//<Text>Hello World</Text>
另一个类可以工作。
有什么建议吗?
最佳答案
你不能像那样渲染多个组件。如错误消息所述,您必须将它们包装在封闭标签中。
<View>
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
</View>
关于javascript - 在 React Native 中显示按钮和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65603106/