gpt4 book ai didi

javascript - React.js 输出未显示

转载 作者:行者123 更新时间:2023-11-30 21:21:04 25 4
gpt4 key购买 nike

我是 React.js 的新手,正在尝试按照一些教程进行操作,并完全按照我在 index.js 中的操作进行操作,但它没有显示我的代码的任何输出。

我使用这两个命令安装 React 并创建

npm install -g create-react-app

create-react-app helloworld

应用仅在 Index.js 文件中创建并执行以下代码,不会干扰任何其他文件,但 H1 不会显示在屏幕上。有什么问题,我做错了什么吗?

这是我的代码

Index.js

import React from 'react';
import ReactDOM from 'react-dom';

class worker extends React.Component{
render()
{
console.log("hello");
return(
<h1> hello farrukh </h1>

)
}
}

ReactDOM.render(< worker /> , document.getElementById('root'))

Index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">

<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>

</body>
</html>

最佳答案

发件人:https://facebook.github.io/react/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized

When an element type starts with a lowercase letter, it refers to a built-in component like or and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

您只需将“worker”重命名为“Worker”即可。

关于javascript - React.js 输出未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45177396/

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