javascript - 在 React Native 中显示按钮和文本

标签 javascript react-native expo

我今天尝试在 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/

相关文章:

javascript - 如何操作 iframe 之外的元素

javascript - 从 JavaScript 引用 Controller 操作

javascript - 在 DHTMLX Scheduler 中添加大量事件时的性能

android - react native android 构建版本代码不更新

javascript - 如何使用纯 JavaScript 获取 div 内图像集的 src 值?

javascript - 在 ListView 中打开和关闭一个可扩展项目 - React Native

ios - 在 React Native 中获取 ScrollView 的当前滚动位置

react-native - 如何在Expo React Native应用程序中检查互联网连接?

ios - React Native 中的 Xcode 项目在哪里?

android - 独立世博项目中的视频播放器