I want to add just React components to Django's template html file (not replace entire template with React as Frontend that there are a lot of tutorial on internet, just certain part of website) because that part of website require high interactivity which is easier to write it in React.
I followed Add React to a Website tutorial to add JSX preprocessor. I set up project like this.
/app-a
/static
/app-a <-- preprocessed files are here
/templates
/app-a
index.html <-- Django template file that I want to include a react component to
/app-b
/react <-- this is where I wrote JSX files
manage.py
package.json
And included React files at the end of index.html file like this.
<html>
<body>
<div id="formContainer">
</div>
<!-- React -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<!-- my root React component file -->
<script src="{% static 'app-a/App.js' %}"></script>
</body>
And I ran babel --watch
to preprocess JSX files like this
npx babel --watch ./react --out-dir ./app-a/static/app-a --presets react-app
I thought it worked because whenever I saved JSX component files, they were preprocessed and output to /app-a/static/app-a
.
At the end of root JSX file, I included root React component to DOM like this.
import React, { Component } from 'react';
... import bunch of components
class App extends Component { ... }
const formContainer = document.querySelector('#formContainer');
ReactDOM.render(App, formContainer);
Unfortunately, when I open website, I get error Uncaught SyntaxError: Unexpected identifier
at import React, { Component } from 'react';
from preprocessed root React component file.
If I delete React
from import statement (directly in preprocessed file), I get error Uncaught SyntaxError: Unexpected token {
and If I delete the first import line, I get the same Unexpected identifier error at next import line.
So, I think web browser have a problem with import
statement.
I thought babel should take care of converting everything in JSX files to JS files that are able to run on web browser.
I'm sure that my JSX files are correct because I wrote these components in Create React App project before I copied all of JSX files to django project.
So, what should I do to add a React component to my website?