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/[email protected]/dist/react-with-addons.js"></script>
  <script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
  <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
  <script src="scripts.js" type="text/babel"></script>
  
</head>

<body>

    <div id="showcontainer"> 
      <div id=app>
      </div>
  </div>
 
</body>
</html>
6
  • 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: create-react-app.dev/docs/getting-started. Edit: I'd recommend skipping template section. Commented Feb 3, 2021 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? Commented Feb 3, 2021 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. Commented Feb 3, 2021 at 22:38
  • @Hoobs okay I understand. thank you so much for helping! Commented Feb 3, 2021 at 22:51
  • 1
    No problem. Which ever way you decide to go, best of luck. :) Commented Feb 3, 2021 at 22:52

1 Answer 1

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/[email protected]/dist/react-with-addons.js"></script>
  <script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
  <script src="https://unpkg.com/[email protected]/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.
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

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.