0

I'm starting to learn reactjs at the moment. I'm wondering how to add normal HTML-Tags in a react-app. Is i just possible to add them by using the render function or can I also just write normal HTML-Tags in my index.html file?

Cause when I'm doing so they're not displayed.

Just like:

const myelement = (<h1>some element</h1>);

ReactDOM.render(myelement, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

  <div>
    <div id="root"></div>
    <div>just normal html</div>
  </div>

Well, it works just fine here.. so there must be something wrong with my build..

2
  • Yep, that's exactly how React works. Show us an example of what you've tried and reproduce how it's not working as expected. Commented Sep 2, 2019 at 19:42
  • @larz Alright, I added some html for understanding. But if it's that easy, okay. :) Commented Sep 2, 2019 at 19:52

2 Answers 2

4

If you're starting out, I recommend you bootstrap your apps using npx create-react-app. It'll give you a good sense of what a React app could look like, and some pointers for file structure.

Most React apps have an index.html file, which you can use like any normal HTML file. But, for the majority of your app, it's recommended to write your content in JSX (otherwise, you aren't getting the benefits of using React in the first place).

JSX

JSX looks very similar to regular HTML, with a handful of key differences:

  • Tag attributes tend to be in lowerCamelCase (onChange rather than onchange)
  • Instead of class (which is a reserved keyword in JavaScript), you need to use className

An Example Component

I've borrowed this sample code from React's official tutorial, which you should definitely check out if you haven't already.

This is a class Component, and your JSX goes inside of the render method:

import React from 'react';

class ShoppingList extends React.Component {
  render() {
    return (
      <div className="shopping-list">
        <h1>Shopping List for {this.props.name}</h1>
        <ul>
          <li>Instagram</li>
          <li>WhatsApp</li>
          <li>Oculus</li>
        </ul>
      </div>
    );
  }
}

What goes in index.html?

The only essential part of index.html is a <div id="root"></div>, which React will use to append the rest of the JSX.

This is also the place to add the usual metadata and icons.

As an example, here's the index.html file that comes with create-react-app. For most of my projects, I leave this pretty-much as-is:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

2

In any given React component, there can only be one parent/top layer html element. You can get around this by using <React.Fragment> ...the rest of your html ... </React.Fragment> (or <>...</> depending on your version) or simply add a wrapping <div> around everything. JSX doesn't distinguish between "normal" html and "React" html, it just turns the React stuff into normal html (over simplification, but close enough for this question). Try it again and let me know if you encounter any problems.

const reactElement = (
  <div>
    React stuff
  </div>
);  

ReactDOM.render(
  reactElement,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div>
  <div id="root">
  </div>
  <div>
    just normal html
  </div>
</div>

4 Comments

Well, when I'm wrapping a normal div element around the root element and adding another normal html tag, it's still not displayed. Just copied your code.
Well, when you click Run code snippet it does work, so the next step would be to show us what you have tried in your code and reproduce the issue here.
Use StackOverflow's code snippets so that you can run live code here.
Well in the snippet it's running. But in my react-app it's not. Even tried setting up a totally new one. Just added an index.html with the code. nothing.

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.