2

I am building MERN project and in react part I got:

- src/
  - components/
    - AdminPage/
      - admin-page.html
      - admin-page.css
      - admin-page.js

So the problem is in App.js:
How could I use regular html, css, js and not ``react in <AdminPage/> component:

import React from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';

import HomePage from './components/HomePage/HomePage.component';
import LoginPage from '../src/components/LoginPage/LoginPage.component';
import RegisterPage from '../src/components/RegisterPage/RegisterPage.component';
import UserPage from '../src/components/UserPage/UserPage.component';
import AdminPage from '../src/components/AdminPage/AdminPage.component';

Is there way NOT to use the classic way like AdminPage.component.jsx and from there export the component; how could I do that with regular html, css and js?

5
  • 1
    let it be a react component and then inside it when the route is hit load html, css and js Commented Dec 20, 2020 at 13:06
  • You might want to update the component naming convention used in your case to AdminPageComponent.jsx inorder for those import statements to work or else used the entire component name as in import AdminPage from '../src/components/AdminPage/AdminPage.component.jsx';. What specific error are you getting though? Commented Dec 20, 2020 at 13:10
  • Oh, so looking closely at your question; you want to avoid using react and just do normla html, css and js, right? Commented Dec 20, 2020 at 13:29
  • @MwamiTovi yes, at the beggining of the project I used html,css and js for everything so I had to transfer everything to react, but that would mean I must transfer 100line html + 200js file in one react file for this component. So I thought maybe there was some other way to do it, because I have html and js already built Commented Dec 20, 2020 at 19:10
  • you could import those files as variables... ie. const HTML = import(../../whatever/the.file/path.is); const JS = import(../again/whatever/the.file/path.is); ok I'm just gonna write it as a new comment Commented Apr 4, 2021 at 18:03

1 Answer 1

1

I'm assuming you don't want to consider a functional component because of maintaining something in the html/css/js that can't be easily refactored into React:

ie.


const NewComponent = () => {

const functions = () => {

}

   return (
     <div style={styles} ></div>


   );
}

const styles = {
...css
}

export default NewComponent;


Then I'd say your best solution would be to paste the body of html from admin-page.html into you index.html file.

Preferably under the root div tag ( ).

This will show up under all your React code (like a footer would) but you can use a class name or id to show / hide the page under certain conditions.

Then just link your css and js as separate stylesheets / scripts in the header and footer.

I would strongly advise against this though as it will most likely increase your technical debt to the project.

Hope it helps : )

[update]

By default, React does not permit you to inject HTML in a component, for various reasons including cross-site scripting. However, for some cases like a CMS or WYSIWYG editor, you have to deal with raw HTML. In this guide, you will learn how you can embed raw HTML inside a component.

dangerouslySetInnerHTML Prop If you try to render an HTML string inside a component directly, React will automatically sanitize it and render it as a plain string.

Ok so looking at this again if you just want to add the complete 200+ lines of HTML / JS I'd say you could just import them to a component with the dangerously SetHTML prop. Like so:

const HTML = import('../the/file/path/to/the.html');
const JS = import('../the/file/path/to/the.js');

const newComponent = () => {
return (
    `<div dangerouslySetInnerHTML={{ __html: myHTML }} />;`
    )

}

Alternatively, If you want a safer solution you could use dompurify:

If XSS is a primary concern, you can use an external library like DOMPurify to sanitize the HTML string before injecting it into the DOM using the dangerouslySetInnerHTML prop.

To install the DOMPurify library, run the following command.

npm i dompurify

You can see the example usage below.

import DOMPurify from "dompurify";

const myHTML = `<h1>John Doe</h1>`;

const mySafeHTML = DOMPurify.sanitize(myHTML);

const App = () => <div dangerouslySetInnerHTML={{ __html: mySafeHTML }} />;

You can also configure DOMPurify to allow only specific tags and attributes.

// ...
const mySafeHTML = DOMPurify.sanitize(myHTML, {
  ALLOWED_TAGS: ["h1", "p", "span"],
  ALLOWED_ATTR: ["style"],
});
// ...

With many thanks to Gaurav Singhal for this brilliant article:

https://www.pluralsight.com/guides/how-to-use-static-html-with-react

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.