简体   繁体   English

反应不渲染?

[英]React not rendering?

I'm trying/playing around with React and have some issues that I cant seem to find on google and there's nothing in my console log? 我正在尝试/使用React,并且遇到了一些似乎无法在Google上找到的问题,并且控制台日志中没有任何内容? Can anybody tell me why this isn't working? 谁能告诉我为什么这不起作用? React and ReactDOM libraries are included... 包括React和ReactDOM库...

HTML 的HTML

<div id="app"></div>

JS JS

class App extends React.Component{
  render(){
    return (
       <div>Hello</div>
    )
  }
}

ReactDOM.render(App, document.getElementById('app'));

您应该渲染JSX元素:

ReactDOM.render(<App />, document.getElementById('app'));

You need to use: 您需要使用:

ReactDOM.render(<App />, document.getElementById('app'));

You can't render a react component class, you need to render an element 您不能渲染一个React组件类,您需要渲染一个元素

Just so you understand why this is the case: 只是您了解为什么会这样:

When you use JSX to create an element your build process will transform it from 当您使用JSX创建元素时,您的构建过程会将其转换为

<App />

to

h("App", {}, null)

But If you just pass the class this conversion will not happen 但是,如果您仅通过课程,则不会发生这种转换

First You have to import the component: 首先,您必须import组件:

import App from "./App";

Then render the component like <App /> : 然后渲染<App />这样的组件:

ReactDOM.render(<App />, document.getElementById("app"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM