You don’t need webpack / Rollup / Babel / whatever to start with React

A common misconception about React is that you need to set up an entire toolchain to get started with it. While that might have been true in the past, that certainly isn’t the case today.

From the React Docs:

The majority of websites aren’t, and don’t need to be, single-page apps. With a few lines of code and no build tooling, try React in a small part of your website. You can then either gradually expand its presence, or keep it contained to a few dynamic widgets.

Basically you need to do three things:

  1. Load React by including a prebuilt version
  2. Create a target container
  3. Render your app inside the container

Like so:

<!-- 1. Load React -->
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js" crossorigin></script>

<!-- 2. Create a target container -->
<div id="root"></div>

<!-- 3. Render your app inside the container -->
<script>
    ReactDOM.render(
        <p>Hello from React {React.version}!</p>,
        document.getElementById('root')
    );
</script>

💡 This approach also works fine for legacy projects where you want to sneak in some React 🙂

This way you can jump right in and get started with it. It’s only later – for example when you start shipping into production and have no control over the browsers being used – that you might need Babel and the lot. You can of course always use create-react-app which takes away the hassle of setting up your React Dev Environment.

👨‍🏫 This topic is also covered in my workshop React from Scratch. If you’re new to React I’d heartedly recommend it 😉

Did this help you out? Like what you see?
Thank me with a coffee.

I don't do this for profit but a small one-time donation would always put a smile on my face. Thanks!

☕️ Buy me a Coffee (€3)

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.