使用React和Django处理许多HTML文件

huangapple go评论71阅读模式
英文:

handle many html files with React and Django

问题

我有一个混合的Django + React实现。Django渲染初始HTML,然后React接管。但是,URL由Django管理。

在React中,index.js文件应该对应一个HTML文件,对吗?如果我有多个需要React的HTML文件,哪种方法是正确的?我的index.js文件如下所示:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { EfestoComponent, WorkTimeComponent, AresComponent } from './components/pulseHome';
import { PulseComponent } from "./components/pulseHistory";

import reportWebVitals from './reportWebVitals';

const efestosData = JSON.parse(document.getElementById('efestos-data').textContent);

const efesto = ReactDOM.createRoot(document.getElementById('efesto'));
const worktime = ReactDOM.createRoot(document.getElementById('worktime'));
const ares = ReactDOM.createRoot(document.getElementById('ares'));

const pulse = ReactDOM.createRoot(document.getElementById('pulse'));

efesto.render(
  <EfestoComponent objects={efestosData}/>
);

worktime.render(
    <WorkTimeComponent/>
)

ares.render(
    <AresComponent/>
)

pulse.render(
    <PulseComponent/>
)

正如您所看到的,efesto、worktime和ares组件属于一个名为pulseHome的js文件,它包含了一个名为pulse_home.html的文件的组件。然后,pulse组件位于一个名为pulseHistory的js文件中,它包含了一个pulse_history.html的组件。当我访问这两个HTML文件中的任何一个时,它会引发错误:

Uncaught TypeError: document.getElementById(...) is null

当然,这是因为第一个元素在第二个HTML中不存在,反之亦然。我可以通过一些巧妙的代码来解决这个问题(AI建议创建另一个index2.js文件),但肯定有一种正确和标准的方法。我对React有点新。任何建议都将有所帮助。

英文:

I have a hybrid django+react implementation. Django renders the initial HTML, then react takes control. However, the URLs are managed by Django.

In react, the index.js file is supposed to correspond with one html right? Please correct me if I'm wrong. So, which is the correct approach if I have several HTML files that need react. My index.js file looks like this:

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import &#39;./index.css&#39;;
import { EfestoComponent, WorkTimeComponent, AresComponent } from &#39;./components/pulseHome&#39;;
import { PulseComponent} from &quot;./components/pulseHistory&quot;;

import reportWebVitals from &#39;./reportWebVitals&#39;;

const efestosData = JSON.parse(document.getElementById(&#39;efestos-data&#39;).textContent);

const efesto = ReactDOM.createRoot(document.getElementById(&#39;efesto&#39;));
const worktime = ReactDOM.createRoot(document.getElementById(&#39;worktime&#39;));
const ares = ReactDOM.createRoot(document.getElementById(&#39;ares&#39;));

const pulse = ReactDOM.createRoot(document.getElementById(&#39;pulse&#39;));


efesto.render(
  &lt;EfestoComponent objects={efestosData}/&gt;
);

worktime.render(
    &lt;WorkTimeComponent/&gt;
)

ares.render(
    &lt;AresComponent/&gt;
)

pulse.render(
    &lt;PulseComponent/&gt;
)

As you can see, the efesto, worktime and ares components belong to 1 js file called pulseHome, it holds the components for a file called pulse_home.html. Then the pulse component lives in a pulseHistory js file which holds the components for a pulse_history.html. When I visit any of the two htmls, it raises the error:

Uncaught TypeError: document.getElementById(...) is null

of course, since the first elements are not present in the second html, and vice versa. I could solve it with some hacky code (the AI suggests creating another index2.js file), but there must be for a correct and standard approach. I'm kind of new to React. Any advice will help.

答案1

得分: 3

  1. 使用服务器上的统一元素ID来创建HTML文件,就像React一样。默认情况下,React会查找带有app ID的div组件来渲染应用程序。您可以在服务器上生成HTML文件时使用唯一的ID集合。例如,您可以使用efestos-data ID,这在所有HTML文件中都有意义。

  2. 如果第一种选项对您不可行,您可以迭代初始文档以获取动态ID,并使用它们来渲染您的React组件。这将根据服务器生成初始HTML文件的方式而有所不同。

就个人而言,我认为您可能在错误的方式下使用React,也许您可以重新考虑整个设计,如果需要服务器端渲染并且想要使用React,也许可以考虑使用nextjs

英文:

I can suggest two solutions that may help you here:

  1. Use unified element ids on the server while creating your HTML file, as React does. React, by default, looks for the div component with the app id to render the application. You can use unique sets of ids for generating your HTML files on the server. For example, you must use efestos-data id that would make sense to be present in all your HTML files

  2. If the first option is not possible for you, you can iterate the initial document to get the dynamic ids and use them to render your React components. Which would be tricky based on how your initial HTML files are generated by the server.

Personally, I think you are using React in the wrong way, maybe you could rethink your whole design and if you need Server Side Rendering and would like to use React as well, maybe consider using nextjs.

答案2

得分: 1

根据什么是REST的说法:

客户端-服务器设计模式强制实现关注点的分离,这有助于客户端和服务器组件独立演进。

您的React应用程序必须负责前端部分和渲染HTML页面。因此,即使是单个页面,让Django为您执行这项任务也不是一种温和的处理方式。您可以简单地为可能需要执行的所有操作实现这一点。

英文:

Based on What is REST that
says:

> The client-server design pattern enforces the separation of concerns, which helps the client and the server components evolve independently.

Your React application must be responsible for the Front-End part and rendering HTML pages. So having Django doing that for you even a single page is not a gentle way to handle that. You can simply achieve that for everything you may want to do.

huangapple
  • 本文由 发表于 2023年6月13日 04:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定