Giter Club home page Giter Club logo

django-jsx-loader's Introduction

Install :

pip install django-jsx-loader

install npm dependencies

npm install --save-dev babel-loader @babel/preset-env @babel/preset-react webpack-cli react react-dom

Setup :

# settings.py
INSTALLED_APPS = [
    ...
    'django_jsx_loader',
    ...
]

...

TEMPLATES = [
    {
        'BACKEND': 'jsxloader.backends.JSXLoaderEngine',
        ...
    },
]

Configuration :

You can change configuration inside django's settings.py

**All paths are relative to the base directory of the project(where manage.py is)

# settings.py

JSX_LOADER = {
    'base_dir': 'frontend',
    'output_dir': 'output',
    ...
}

Configuration Options :

Configration Type Description Default
base_dir String The name of the directory housing all other directories related to the jsx loader. jsx_modules
output_dir String The name of the directory where the output javascript files will be placed. static

Usage :

  • Inline JSX :

<!-- template.html -->
{% load jsx_loader %}
{% JSX %}
    <div className="Component">
        <h1>Hi, I'm a React App</h1>
    </div>
{% endJSX %}
  • Jsx Component File :

<!-- template.html -->
{% load jsx_loader %}
{% JSXComponentFile 'components/counter.jsx' %}
  • Inline Jsx Component :

<!-- template.html -->
{% load jsx_loader %}
{% JSXComponent %}
    import React, { useState } from 'react';

    const Counter = () => {
        const [count, setCount] = useState()
        return (
            <div className="Counter">
                <h1>Hi, I'm a React Counter</h1>
                <h2>{count}</h2>
                <button onClick={() => setCount(count + 1)}>+</button>
            </div>
        );
    }

    export default Counter;
{% endJSXComponent %}

Loading Steps :

  • Tag Content is copied to a temp file

      <!-- temp file -->
      const componentID = () => {
          return (
              <!-- Copied JSX from JSX tag -->
              <div className="Component">
                  <h1>Hi, I'm a React App</h1>
              </div>
          );
      }
    
      export default componentID;
    
  • Component Rendering Script Is Made

    import JSXComponent from 'temp.component.file'
    import AnotherComponent from 'another.temp.file'
    import reactDOM from 'react-dom'
    
    reactDOM.render(<JSXComponent />, document.getElementById('component-id'))
    reactDOM.render(<AnotherComponent />, document.getElementById('another-component-id'))
    
  • Tags are replaced with placeholder HTML elements.

    <div id="component-id"></div>
    
  • Webpack Config file is made with Rendering Script as entry point

  • webpack command is run

  • output script file is added to the template

    <script src="{% static 'output.js' %}"></script>
    

**All the steps should be done manually in production by running a command except for tags replacement.

django-jsx-loader's People

Contributors

abdallah-ragab avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.