1

I want to use React.js for my portfolio and I'm having trouble rendering my code into HTML. I am trying to list my projects using images and p tags displaying the title of each project and the role I played. Also, I want the React elements to be clickable, going to the link of each project. I don't know what is wrong with my code so far.

import React from 'react';
import ReactDOM from 'react-dom';
import './styles.css'; 

'use strict';

function Project(props) {
    return React.createElement("div", {
      className: "workbox"
    }, React.createElement("img", {
      src: props.img
    }), React.createElement("p", null, props.title), 
    React.createElement("p", null, props.role));
  }
  
  var app = React.createElement("div", null, 
  React.createElement("a", {href: "moonlight.html"}, React.createElement(Project, {img: "images/moonlight.png", 
  title: "Moonlight Website Concept", role: "UI Designer"})), 
  
  React.createElement("a", {href: "dailyui.html"}, React.createElement(Project, {img: "images/dailyui.png",
  title: "DailyUI Challenge", role: "UI Designer"})), 
  
  React.createElement("a", {href: "poetry.html"}, React.createElement(Project, {img: "images/theartofchoice.png", 
  title: "Poetry",role: "Writer"})));

  ReactDOM.render(app, document.querySelector('#app'));

HTML

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
 

  <link rel="stylesheet" href="styles.css">
  <script src="https://unpkg.com/react@15.4.1/dist/react-with-addons.js"></script>
  <script src="https://unpkg.com/react-dom@15.4.1/dist/react-dom.js"></script>
  <script src="https://unpkg.com/babel-standalone@6.19.0/babel.min.js"></script>
  <script src="scripts.js" type="text/babel"></script>
  
</head>

<body>

    <div id="showcontainer"> 
      <div id=app>
      </div>
  </div>
 
</body>
</html>
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sylvia B
  • 15
  • 3
  • I really hope I'm not coming off the wrong way, sorry if I am, but your environment looks very different to the typical React environment. I'd highly recommend following these instructions to create your first React application: https://create-react-app.dev/docs/getting-started/. Edit: I'd recommend skipping template section. – Michael Hoobler Feb 03 '21 at 21:03
  • @Hoobs I originally started with an HTML and CSS file so I was trying to use react outside of create-react-app. Do you know any resources to do that so I don't have to start over? – Sylvia B Feb 03 '21 at 22:09
  • 1
    I understand, but I really really think you're going to run into a lot more problems and struggle unnecessarily in the long run doing things so differently. I can't really give you any guidance because I don't know how React works without an environment and I've never run into any resource that had React running without some kind of environment. The create-react-app environment basically adds stuff on top of HTML/CSS, so you'll be able to copy and paste your code over pretty easily once you get a feel for it. I don't mean to be so "you can't do things differently!", just wanna help. – Michael Hoobler Feb 03 '21 at 22:38
  • @Hoobs okay I understand. thank you so much for helping! – Sylvia B Feb 03 '21 at 22:51
  • 1
    No problem. Which ever way you decide to go, best of luck. :) – Michael Hoobler Feb 03 '21 at 22:52
  • modules (import / export) will cause touble, but for ReactJS in HTML this answer might help: [How to use React components as part of legacy HTML/JS app?](https://stackoverflow.com/questions/65917670/how-to-use-react-components-as-part-of-legacy-html-js-app/65945804#65945804) – kca Feb 04 '21 at 19:54

1 Answers1

1
  1. you need to call ReactDOM.render(app, document.querySelector('#app')); only when the <div id=app> is ready. Put your <script> tags at the end of the body:
...
  <script src="https://unpkg.com/react@15.4.1/dist/react-with-addons.js"></script>
  <script src="https://unpkg.com/react-dom@15.4.1/dist/react-dom.js"></script>
  <script src="https://unpkg.com/babel-standalone@6.19.0/babel.min.js"></script>
  <script src="scripts.js"></script>
</body>
  1. remove the imports.
    If React is imported via <script> tag, you don't need the imports. import doesn't work for "normal" html pages, it works only for Javascript-modules, which requires a server an a lot of setup.
kca
  • 4,856
  • 1
  • 20
  • 41
  • Exactly, you can't import the modules using "import" if you aren't using react directly from your server right? I'm new to react just curious. – Love2Code Feb 04 '21 at 20:33
  • 1
    You would import JS-modules with `import`, but you are not using JS-modules. And it's also not only using a server, there is usually more to setup (e.g. webpack). JS-modules is a whole big topic on it's own. See [MDN - JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). – kca Feb 04 '21 at 20:53