We believe intertia.js is a very good library for building web-apps with your favorite front-end library without compromising your productivity without complex rest-endpoints for simple application. Using Inertia.js you can build a traditional monolith with laravel/rails and bind the view layer with React/Vue/Svelte.
Our team love inertia.js but the inavailablity of it's support to django led us develop a new library similar to inertia.js that works with django.
Metro.js supports react 16.
- Metro.js consists of a django Adapter that helps to render the react_base.html.
- Metro.js needs webpack setup and has dependency for turbolinks to make routing.
- react_loader.js file consists of bootstrap() that is the entry point of the metro.js app.
- Pages are placed in page folder. props.params helps you access django template variables.
- Add the metro-js folder into root static folder.
- Add the react and react-dom.
- Setup webpack and webpack-cli
- Add metro_adapter.py to your root django-app.
const path = require('path');
module.exports = {
entry: {
react_loader: './src/react_loader.js'
},
mode: 'development',
output: {
filename: '[name].js',
path: path.resolve('./', 'generated')
},
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}]
}
}
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
import React from "react"
import ReactDOM from "react-dom"
import {NotFound} from "./pages/notfound";
import {Home} from "./pages/home";
const pageName = document.getElementById('page-name').innerHTML;
const pageParams = document.getElementById('page-params').innerHTML;
// Your routes go here
function assignPage(pageName) {
let PageComponent = NotFound;
switch (pageName) {
case 'home':
PageComponent = Home;
break;
default:
PageComponent = NotFound;
break;
}
return PageComponent;
}
function bootstrap() {
let PageComponent = assignPage(pageName);
ReactDOM.render(
<PageComponent params={JSON.parse(pageParams)}/>,
document.getElementById("react-root")
)
}
bootstrap();
Todo Example
Props
-
method (GET/POST/PUT/DELETE)
-
data (form data)
-
action - URL of
-
afterSubmit - receives 2 arguments -> res (response), err (error). Response is null when error and vice-versa
<MetroForm method="POST" action="/todos/" data={{name: newTodo}} afterSubmit={(res,err)=>{console.log(res);console.log(err)}} > <h1>Create Todo</h1> <input onChange={(e)=>{setNewTodo(e.target.value)}} placeholder="Enter Value"/> <button value="submit">Create</button> </MetroForm>
Metro Station handle routes between your react pages.
<MetroStation to="/todos/">Go To Todos</MetroStation>
- React 16+
-
Turbolinks 5.2 - Axios 0.19
- Removed external dependency on Turbolinks.
This is a initial version of the library. Use at your own risk in production.
Made by Harish with ❤️ from India.