• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

react router v4 async routing

Async Routing

TODO:

  • server render
  • make route config optional

React router v4 is awesome, but one thing is missing! Async routes. There's a couple components out there but they all have the same problem, on route change, the component will return null, then load the page. This makes for an annoying user experience. This package will wait for the bundle before a route change, and also allow you to preload pages!

You must be using webpack 2 along with syntax-dynamic-import to use this package!

Install

npm install react-router-async-routing

Setup

Create a routes file, with the pathname and path to your component files

export default [
  {
    path: '/employers',
    dataPath: 'employers',
  },
  {
    path: '/test/:name',
    dataPath: 'test/index',
  },
]

Create a router file that will export Preload, Link, and Route components for you to use in place of the built in components in v4:

import routes from './routes';
import AsyncSetup from 'react-router-async-routing'

const { Route, Link, Preload } = AsyncSetup(routes, path => import(`./views/${path}.js`));

export { Link, Route, Preload };

The first argument is your routes array, the second is a function that takes a path (the dataPath in the routes file) and load the component according to its location.

Now, render your routes using the components from the file you just created!

import { Route } from './router'

....
{routes.map(route => <Route key={route.path} {...route} />)}

Now, you can link to any page like you're used to, just import { Link } from './router'!

Preloading routes

Note: If you use a service worker, this isn't really needed. Except on older browsers.

If a user is on the homepage, and you'd like to load the about page ahead of time, just render this inside of your homepage:

export const Home = (
  <div>
    <h2>Home</h2>
    <Preload path="/about" />
  </div>  
)

The route will be loaded in the background :)

Custom loading

If you need to do more than just async load a route, you can make your own loader by importing components directly:

import LinkBuilder from 'react-router-async-routing/link'

const CustomLink = LinkBuilder(
  async ({ path }) => await somePromise()
)

The function passed will be awaited on any time a link is clicked.

More Repositories

1

validify

Simple-as-possible React form validation
TypeScript
281
star
2

cli.rs

use {yourproject}.cli.rs!
275
star
3

gutenblock

The easiest way to develop and release Gutenberg blocks (components) for WordPress
JavaScript
239
star
4

react-calendar

Simple and flexible events calendar written in React
TypeScript
142
star
5

reactview

reactview lets you instantly view (and hot reload) any react component in your browser
JavaScript
96
star
6

react-native-autofocus

Autofocus the next input field on enter in React Native
JavaScript
86
star
7

use-upload

Framework agnostic library handling file uploads with progress, direct to AWS / GCP, and other locations
TypeScript
77
star
8

match

a port of Rust's match in JS
JavaScript
77
star
9

formjs

A form generator for Reactjs "Alpachajs for React" Open the inspector on the demo page:
JavaScript
49
star
10

react-slug

A React component that will turn any input into a URL friendly slug!
JavaScript
23
star
11

graphql-course

Code from ultimatecourses
JavaScript
17
star
12

moovite

TypeScript
15
star
13

Simpler

A simple react / angular clone. Well, that's the end goal anyway. Just an experiment.
JavaScript
11
star
14

react-runner

build react apps with minimal setup WIP
JavaScript
10
star
15

Spot

a horribly coded thingy using contenteditable attributes
PHP
9
star
16

stock-api

get latest stock price data using puppeteer and google
JavaScript
8
star
17

redux-restore

Handles storage in React Native
JavaScript
8
star
18

flydb

nosql, in memory database for json documents
Rust
6
star
19

nexus-prisma-boilerplate

Get started with Nexus and Prisma quickly
TypeScript
6
star
20

legit.js

a lightweight javascript framework for modifying the DOM / CSS
JavaScript
6
star
21

passwordless

[Beta] Open source webauthn hardware key implementation for Node + JS
TypeScript
5
star
22

nodechat

working on making a full on live chatroom with node.js and socket.io
JavaScript
4
star
23

reason-apollo-server

Set of bindings for Apollo Server in ReasonML
OCaml
4
star
24

restor

simpler-than-redux react state management
JavaScript
3
star
25

React-Generator

quickly create components and get to coding!
JavaScript
3
star
26

vscode-sort-by-length

Sort imports by line length in VS Code!
TypeScript
3
star
27

render-placement-loader

Add React.render to your component automatically (used in reactview)
JavaScript
2
star
28

webpack-force-hash-plugin

Only changes the vendor hash if needed
JavaScript
2
star
29

react-easy-test

need a better name... scroll down and see what this is!
JavaScript
2
star
30

memeify

A slack bot that generates memes from photos you choose!
JavaScript
2
star
31

suspense-fetch

experimental useFetch with suspense concurrent mode
JavaScript
1
star
32

action.js

a mini library for turning form requests into ajax
JavaScript
1
star
33

stream

a simple node / react app for displaying live streams
JavaScript
1
star
34

iBlog

a PHP project I made back when I was 15 (5 years ago) woah....
PHP
1
star
35

hmr-apollo-server

Apollo server 2 with hot module replacement
JavaScript
1
star
36

homie

iOS app that turns on Philips Hue lights using iBeacons (WIP)
Swift
1
star
37

huetron

a desktop app for managing your hue lights
JavaScript
1
star
38

class

js / node coding course
JavaScript
1
star
39

react-suspendable

a function that can wrap any async action and make it compatible with suspense
TypeScript
1
star
40

WProgress

Reading progress bar and other readability enhancements for your Wordpress blog
JavaScript
1
star
41

react-state-management

Created with StackBlitz ⚡️
JavaScript
1
star