简体   繁体   English

是否可以将 React 代码转换为 HTML、CSS、JavaScript?

[英]Is it possible to convert React code to HTML, CSS, JavaScript?

I'm trying to make a website with React without any back-end, I would only like to use the components and stuff like that.我正在尝试使用 React 制作一个没有任何后端的网站,我只想使用组件和类似的东西。 I also want to convert it to a HTML, CSS, and JavaScript files because it is much easier for me to publish it like that.我还想将其转换为 HTML、CSS 和 JavaScript 文件,因为这样发布它更容易。 I heard that Angular has a feature like this, but I'm not sure if React has it.听说Angular有这样的功能,但不确定React有没有。

If there is no way to do it, can someone just respond that it is not possible?如果没有办法做到这一点,有人可以回答说不可能吗? Also, if it is actually possible in Angular than how to do it?另外,如果在Angular中真的可以比怎么办呢? I might learn Angular for it.我可能会为此学习 Angular。

I know it's just way easier to upload it as a React app, but I can't do that, so I need a HTML, CSS and JavaScript file for it.我知道将它作为 React 应用程序上传更容易,但我不能这样做,所以我需要一个 HTML、CSS 和 JavaScript 文件。 I am also a beginner, so I don't understand what they mean I should do in my code.我也是初学者,所以我不明白他们的意思是我应该在我的代码中做什么。 I ran我跑了

npm run build

However, when I tried to put it on my domain, it didn't work, this is what i see但是,当我尝试将其放在我的域中时,它不起作用,这就是我所看到的

And this is the structure I have on CPanel.这就是我在 CPanel 上的结构。 Somehow I get no result, before the npm run build command this was code (which worked):不知何故,在 npm 运行构建命令之前,我没有得到任何结果,这是代码(有效):

my app.js:我的 app.js:

import React, { useState } from 'react';
import FlashcardList from './FlashcardList'
function App() {
  const [Flashcards, setFlashcards] = useState(HIRAGANA_FLASHCARDS)
  return (
    <FlashcardList flashcards={Flashcards} />
    );
}
const HIRAGANA_FLASHCARDS = [
  //bunch of data
]
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

// Used like so
shuffle(HIRAGANA_FLASHCARDS);
export default App;

my flashcardlist.js:我的抽认卡列表.js:

import React from 'react'
import Flashcard from './Flashcard'

export default function FlashcardList({flashcards}) {
    return (
        <div className="card-grid">
            {flashcards.map(flashcard => {
                return <Flashcard flashcard={flashcard} key={flashcard.id}/>
            })}
        </div>
    )
}

and my flashcard.js:和我的抽认卡.js:

import React, {useState} from 'react'
import './main.css'
export default function Flashcard({flashcard}) {
    const [Flip, setFlip] = useState(false)
    return (
        <div className="text" onClick={() => {
            setFlip(!Flip)
        }}>
            {Flip ? flashcard.answer : flashcard.question}
        </div>
    )
}

It sounds like you're referring to static rendering, which is a feature of React.听起来你指的是 static 渲染,这是 React 的一个特性。 This is how server side rendering (SSR) is accomplished.这就是服务器端渲染 (SSR) 的完成方式。 The React app is rendered on the server, converted to HTML using ReactDOM.renderToString(), then sent to the client. React 应用程序在服务器上呈现,使用 ReactDOM.renderToString() 转换为 HTML,然后发送到客户端。

Normally there is a "rehydration" on the client where the App then re-renders using javascript, but that is not required.通常在客户端上会有一个“补水”,然后应用程序使用 javascript 重新渲染,但这不是必需的。 You could serve the HTML statically and never "hydrate" the app on the client.您可以静态地为 HTML 提供服务,并且永远不要在客户端上“水合”应用程序。

In fact, this is the approach I'm using for certain routes in my App that do not need to be dynamic.事实上,这是我在我的应用程序中用于某些不需要动态的路由的方法。 I "statically render" that route and save the output to a.html file, which my server then sends to the client when that route is requested as if it were only ever a statically created.html file.我“静态渲染”该路由并将 output 保存到 a.html 文件,然后当请求该路由时,我的服务器将其发送给客户端,就好像它只是静态创建的一样。ZFC35FDC70D5FC69D7A533E 文件。

See React's documentation on this here在此处查看 React 的文档

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

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