Skip to content

Setup React

Install Dependency

Go to frontend directory

$ npm install @babel/preset-react eslint-plugin-react --save-dev

Once done, edit frontend/.babelrc to make babel to use the above @babel/preset-react

  "presets": [
        "useBuiltIns": "usage",
        "corejs": "3.0.0"
  "plugins": [

Edit .eslintrc to add plugin:react/recommended to the extends, so eslint can recognize React syntax.

  "parser": "babel-eslint",
  "extends": [
  "env": {
    "browser": true,
    "node": true
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
  "rules": {
    "semi": 2

Next, let's install React

$ npm install react react-dom

That is it, now the frontend project can work with React.

Sample App

Update frontend/src/application/app2.js

import React from 'react';
import ReactDOM from 'react-dom';

class Clock extends React.Component {
  constructor(props) {
    this.state = {date: new Date()};

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),

  componentWillUnmount() {

  tick() {
      date: new Date()

  render() {
    return (
        <h1>Hello, world!</h1>
        <h2>It is {}.</h2>

  <Clock />,

This is a simple Clock React component come from React doc


You can also put the React components code to the src/components and import the component in entryfiles. (app2.js in this case)

$ npm run start

Edit Django template templates/index.html

{% load webpack_loader static %}

<!DOCTYPE html>
  <meta charset="utf-8" />
  {% stylesheet_pack 'app' %}

<div class="jumbotron py-5">
  <div class="container">
    <h1 class="display-3">Hello, world!</h1>
    <p>This is a template for a simple marketing or informational website. It includes a large callout called a
      jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
    <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more ยป</a></p>

    <div class="d-flex justify-content-center">
      <img src="{% static 'vendors/images/webpack.png' %}" class="img-fluid"/>


<div id="root">

{% javascript_pack 'app' 'app2' %}

  1. We added a root div to let React render the component.
  2. We use {% javascript_pack 'app' 'app2' %} to load two entrypoint files to the template.
$ python runserver


Here we use React to render specific component in the page, and we can still use raw HTML to write other parts, which is convenient

Here is the screenshot:

React example