简体   繁体   中英

How to render React.js into HTML for portfolio

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>
  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 import s.
    If React is imported via <script> tag, you don't need the import s. import doesn't work for "normal" html pages, it works only for Javascript-modules , which requires a server an a lot of setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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