0

I've been trying to run some of the basic React examples in Electron App, but nothing shows up, even though there is no errors.

Here's a file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <div id="root"></div>

    <script type="text/babel">
      const React = require('react');
      const ReactDOM = require('react-dom');

      ReactDOM.render(
        <h1>Hello World</h1>,
        document.getElementById('root')
      );
    </script>
  </body>
</html>

I have the following packages installed: react, react-dom and electron. Am I doing something wrong? Thanks in advance!

0

1 Answer 1

2

type="text/babel" isn't recognized by browsers as a valid script type, so it'll just ignore it and skip over it completely, hence the lack of any error. You need to include the babel script to make it parse, like so:

<body>
  <div id="root"></div>

  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  <script type="text/babel">
    // your react code here
  </script>
</body>

This is fine for exploration and prototyping, but it's not good if you want to release the app for others to use. Having babel parse the script on-the-fly can weigh on performance and memory usage.

Instead, look into a tutorial on how to pre-compile your scripts and run them that way. This boilerplate could serve as a good reference.

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

Comments

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.