0

I'm trying to work React into my HTML page which I'm trying to create dynamic cards from bootstrap, but I keep getting Here's what I have so far

service.html

<head>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="services-load"></div>
<script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>

  <script type="module" src="rjs.js"></script>
</body>

rjs.js

const e = React.createElement;
import Card from "./services/Cards";

const domContainer = document.querySelector('#services-load');
ReactDOM.render(e(<Card />), domContainer);

Cards.js

import Card from "./CardUI";
import img1 from "../assets/imgs/services/botox.jpg";
import img2 from "../assets/imgs/services/lipo.jpg";
import img3 from "../assets/imgs/services/prp.jpg";

const Card = props => {
  return (
    <div className="container aesthetic-body">
      <div className="row">
        <div className="col-lg-4 col-md-4 col-sm-4 col-xs-4">
          <Card imgsrc={img1}/>
        </div>
        <div className="col-lg-4 col-md-4 col-sm-4 col-xs-4">
          <Card imgsrc={img2}/>
        </div>
        <div className="col-lg-4 col-md-4 col-sm-4 col-xs-4">
          <Card imgsrc={img3}/>
        </div>
      </div>
    </div>
  );
}

export default Card;

CardUI.js

const Card = props => {
  return (
    <div className="card">
        <a href="">
          <div className="card-header">BOTOX / FILLERS</div>
          <img className="card-img-top" src={props.imgsrc} alt={"Card image cap"}/>
        </a>
      </div>
  );
}

export default Card;

Update: Here's My file structure

3 Answers 3

1

In order to work with import, you need to add a plugin for transforming transform-es2015-modules-umd by specify the plugins on <script />.

Let's imagine you have a following structure:

public
 - foo.js
 - index.js
 - index.html

foo.js exports an simple component like this:

export default () => <div>Foo</div>;

index.js is all about importing and boostrap:

import Foo from "foo";

ReactDOM.render(<Foo />, document.getElementById("root"));

Finally, register modules in index.html:

<script
  data-plugins="transform-es2015-modules-umd"
  type="text/babel"
  src="./foo.js"
></script>
<script
  data-plugins="transform-es2015-modules-umd"
  type="text/babel"
  src="./index.js"
></script>
</body>

Here is full code in index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script
      src="https://unpkg.com/react@16/umd/react.production.min.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"
      crossorigin
    ></script>

    <title>React App</title>
  </head>

  <body>
    <div id="root"></div>
    <script
      data-plugins="transform-es2015-modules-umd"
      type="text/babel"
      src="./foo.js"
    ></script>
    <script
      data-plugins="transform-es2015-modules-umd"
      type="text/babel"
      src="./index.js"
    ></script>
  </body>
</html>

Also the codesanbox link: https://codesandbox.io/s/quizzical-bird-vwn5t?file=/public/index.js

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

9 Comments

hello I'm getting Uncaught Error: Invalid plugin specified in Babel options: "transform-es2015-modules-umd" I've installed npm install --save-dev babel-plugin-transform-es2015-modules-umd but still nothing
You don’t need to install anything, you just copy and run code via the browser
If you are not keen to browser anymore, you can simply switch to webpack which is quite easy for configuration. I thought you’re interested in playing with browser :)
Thank you for that! But im still getting the invalid plugin. I saw your package.json file and mine looks different. Sorry I'm still learning how to use react thank you for baring with me. "name": "clinic45", "version": "1.0.0", "description": "", "main": "rjs.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@babel/core": "^7.11.6", "@babel/preset-env": "^7.11.5", "@babel/preset-react": "^7.10.4", "webpack-cdn-plugin": "^3.3.1" } }
I’m not sure I’m guessing right. Do you mean you don’t know how to configure webpack?
|
0

You can't write HTML on js file. you need to switch to jsx ( Javascript XML ) file format.

Rename your files to Cards.js => Cards.jsx and CardUI.js => CardUI.jsx

2 Comments

I did that before and did it now again same error :/
please follow proper document of converting babel code to pure javascript
0

I think you need a babel transpiler to translate the JSX syntax.

https://babeljs.io/

for react : https://babeljs.io/docs/en/babel-preset-react

2 Comments

I'm using this <script src="unpkg.com/@babel/standalone/babel.min.js"></script> shouldn't that translate?
it should translate but not for standalone react components like CardUI.jsx or CardUI.js. so you need babel to translate CardUI.js You can view it here: reactjs.org/docs/…

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.