javascript - 为什么我在使用 React-Web 时遇到这些错误

标签 javascript reactjs typescript react-hooks react-web

所以我正在尝试完成一个 React Web 教程来学习如何制作 Android 和 iOS 应用程序,并且我一直在关注本教程:https://www.youtube.com/watch?time_continue=469&v=_CBYbEGvxYY 但是当我尝试运行一个简单的页面来测试一些钩子(Hook)时:

import React, { useState } from 'react';
import logo from './logo.svg';
import { TextComponent } from 'react-native';

const App = () => {
  const[count, setCount] = useState(0);
  return (
    <view>
      <text>{count}</text>
      <button title="Increment" onKeyPress={()=> setCount(count + 1)}>Increment</button>
    </view>
  );
}

export default App;

我收到以下控制台错误:

Warning: The tag <text> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.
    in text (at App.tsx:9)
    in view (at App.tsx:8)
    in App
    in div (created by View)
    in View (created by AppContainer)
    in div (created by View)
    in View (created by AppContainer)
    in AppContainer

程序根本不会增加计数。我还尝试将标签名称更改为大写,如错误所示,但这不起作用,因为标签无法识别。有人可以帮帮我吗? 注意:我很困惑为什么现在会收到这些错误,因为之前我只有一个带有文本的简单 View 时,程序运行正常,但现在我收到了这些错误......

最佳答案

没有名为 <text> 的标签在 HTML 中。在视频中,他们导入了:

import { View, Text } from 'react-native';

因此,您需要确保导入和使用正确的组件。它们区分大小写:

import React, { useState } from 'react';
import logo from './logo.svg';
import { View, Text } from 'react-native';

const App = () => {
  const[count, setCount] = useState(0);
  return (
    <View>
      <Text>{count}</Text>
      <button title="Increment" onKeyPress={()=> setCount(count + 1)}>Increment</button>
    </View>
  );
}

export default App;

关于javascript - 为什么我在使用 React-Web 时遇到这些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58945215/

相关文章:

javascript - 使用 jQuery 从 PHP 关联数组中获取值

javascript - 为什么 React Router Dom 的链接返回时不刷新?

javascript - 检查某些内容是否不为空后,我收到空或未定义类型错误。如何?

javascript - Angular 应用程序 : es2020 migration from es2015

angular - 输入超慢的 ionic 页面

javascript - addEventListener 的闭包应该放在哪里?

javascript - 创建 CSS 过渡

reactjs - React 的无效 Prop 类型 'array' 预期 'object'

引用时未定义 Angular 6 属性指令

javascript - 选择值列表时的浏览器后退按钮