【问题标题】:React.Js not rendering in django projectReact.Js 没有在 django 项目中呈现
【发布时间】:2020-04-28 21:15:46
【问题描述】:

我正在创建一个项目,其中 react 没有在 django localhost 上呈现任何内容

index.html

<!DOCTYPE html>
<html lang="en">
    <head></head>
    <body>
        <div id="App">
            <!---all will be define in App.js-->
            <h1>Index.html </h1>
        </div>
    </body>
    {% load static%}
    <script src="{% static "frontend/main.js" %}"></script>
</html>

app.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Header from './layout/header';

class App extends Component {
    render() {
        return (
        <h1>App.JS</h1>
        )
    }
}

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

这是我的项目结构:

在运行 npm run dev 和 python manage.py runserver 之后,这是一切正常的状态:

【问题讨论】:

  • 您是否尝试将文件 app.js 的名称更改为 App.js?

标签: javascript html django reactjs


【解决方案1】:

更改此源代码:

document.getElementById('app')

...到这个:

document.getElementById('App')

【讨论】:

    【解决方案2】:

    这是因为您的元素的 id 为“App”,但您试图将 react app 挂接到元素“app”上。区分大小写。

    【讨论】:

      【解决方案3】:

      document.getElementById 区分大小写

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 2018-10-08
        • 2012-02-15
        相关资源
        最近更新 更多