• Stars
    star
    196
  • Rank 192,719 (Top 4 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 2 months ago

Reviews

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

Repository Details

Configuration driven routing solution for React SPAs that manages route matching, data fetching and progressive rendering

react-resource-router logo

react-resource-router

React Resource Router (RRR) is a configuration driven routing solution for React that manages single page application route matching, data fetching and progressive rendering.

Why?

React Resource Router was developed by Atlassian for Jira primarily to improve performance and prepare for compatibility with React's forthcoming Concurrent Mode on both client and server. You can read more about its development and impact here.

Features

  • Fully driven by a static configuration of route objects
  • Each route object contains the following core properties
    • path - the path to match
    • component - the component to render
    • resources - an array of objects containing fetch functions that request the route component's data
  • Data for a route is requested asynchronously and as early as possible, with the page progressively rendering as the requests resolve. This results in quicker meaningful render times
  • Works on both client and server without having to traverse the React tree

Usage

Create your resources

Resources describe and provide the data required for your route. This data is safely stored and accessed via the useResource hook or ResourceSubscriber component.

import { createResource } from 'react-resource-router/resources';
import { fetch } from '../common/utils';

export const homeResource = createResource({
  type: 'HOME',
  getKey: () => 'home-resource-key',
  getData: () => fetch('https://my-api.com/home'),
});

export const aboutResource = createResource({
  type: 'ABOUT',
  getKey: () => 'about-resource-key',
  getData: () => fetch('https://my-api.com/about'),
});

Create your components

These are the React components that get rendered for your routes. As mentioned, they can be wired into the state of your resources via the useResource hook or ResourceSubscriber component.

import { useResource } from 'react-resource-router/resources';
import { aboutResource, homeResource } from '../routes/resources';
import { Loading, Error } from './common';

export const Home = () => {
  const { data, loading, error } = useResource(homeResource);

  if (error) {
    return <Error error={error} />;
  }

  if (loading) {
    return <Loading />;
  }

  return <div>{data.home.content}</div>;
};

export const About = () => {
  const { data, loading, error } = useResource(aboutResource);

  if (error) {
    return <Error error={error} />;
  }

  if (loading) {
    return <Loading />;
  }

  return <div>{data.about.content}</div>;
};

Create your routes

Your route configuration is the single source of truth for your application's routing concerns.

import { Home, About } from '../components';
import { homeResource, aboutResource } from './resources';

export const appRoutes = [
  {
    name: 'home',
    path: '/',
    exact: true,
    component: Home,
    resources: [homeResource],
  },
  {
    name: 'about',
    path: '/about',
    exact: true,
    component: About,
    resources: [aboutResource],
  },
];

Use the Router

Now that you've set up your resources, components and configuration correctly, all you need to do is mount the Router in your react tree with a RouteComponent as a child. It will do the rest!

import {
  Router,
  RouteComponent,
  createBrowserHistory,
} from 'react-resource-router';
import { createResourcesPlugin } from 'react-resource-router/resources';
import { appRoutes } from './routing/routes';

const history = createBrowserHistory();
const resourcesPlugin = createResourcesPlugin({});

const App = () => (
  <Router routes={appRoutes} history={history} plugins={[resourcesPlugin]}>
    <RouteComponent />
  </Router>
);

Installation

npm install react-resource-router

# or

yarn add react-resource-router

Documentation

Check the docs website or the docs folder.

Examples

You can checkout the repo and play around with the examples we have setup to demonstrate how the API can be used for various use cases.

  1. Clone the repo and install dependencies
  2. Run npm start
  3. Local dev site will launch with all the examples

Thanks

Big thanks to Thinkmill for their involvement in this project.

License

Copyright (c) 2020 Atlassian and others. Apache 2.0 licensed, see LICENSE file.


With ❤️ from Atlassian

More Repositories

1

compiled

A familiar and performant compile time CSS-in-JS library for React.
TypeScript
1,965
star
2

storybook-addon-performance

🚧 A storybook addon to help better understand and debug performance for React components.
TypeScript
634
star
3

nadel

A GraphQL execution engine for distributed schemas
Kotlin
152
star
4

peerd

peerd is an AWS VPC Peering Connection management tool. It creates full-meshes of VPCs from a yaml file, and manages the full lifecycle of creation, deletion and route table updates needed to make VPC peerings useful across accounts and regions. Contributions welcome.
Python
134
star
5

react-loosely-lazy

Use advanced React async components patterns today
TypeScript
123
star
6

json-schema-viewer

A JSON Schema Viewer for any JSON Schema on earth.
TypeScript
103
star
7

webpack-deduplication-plugin

Plugin for webpack that de-duplicates transitive dependencies in yarn and webpack-based projects.
JavaScript
75
star
8

Flowbius

Kotlin Flow <-> Spotify Mobius interop
Kotlin
57
star
9

atlassian-slack-integration-server

Slack plugins for Jira, Confluence and Bitbucket Server
Java
27
star
10

cyclops

Cyclops Kubernetes Node Auto Cycling Toolset
Go
23
star
11

data-center-grafana-dashboards

18
star
12

babel-plugin-storybook-csf-title

A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
JavaScript
17
star
13

data-center-terraform

This project provides App vendors in Atlassian ecosystem with ready to use environment setup tool for Data Center products.
HCL
17
star
14

connect-security-req-tester

A tool to check your Atlassian Connect app against Atlassian's security requirements.
Python
15
star
15

compass-examples

Example apps and code snippets for Atlassian Compass
Python
14
star
16

Jira-DC-Grafana-Dashboards

12
star
17

jira-release-blocker

Shell
10
star
18

issue-status-helper

An example Forge application for automatically managing Jira issue status
TypeScript
10
star
19

gitlab-for-compass

Compass integration with source code management tool GitLab.
TypeScript
9
star
20

bootgraph

Kotlin
9
star
21

db-replica

Automatically chooses between database connections to read-write main or read-only replica
Java
8
star
22

compiled-website

The documentation website for Compiled (CSS-in-JS).
MDX
7
star
23

msteams-jira-server

Microsoft Teams plugin for Jira Server
C#
6
star
24

Earl

Earl is a tool designed to help estimate how much you spent on DynamoDb in the last week. It can help you tweak your auto scaling settings to help you minimise wastage and compare the cost of provisioned vs. on-demand capacity modes.
Kotlin
6
star
25

inline-require-webpack-plugin

Optimise generated bundles by inline requiring ES modules, without CommonJS deoptimisations
TypeScript
5
star
26

FSRT

A static analyzer for finding Forge app vulnerabilities
Rust
4
star
27

observe

@observe is a Python decorator
Python
4
star
28

transfer-api-ref-client

Reference API implementation that supports large file uploads (>100GB) in a chunked and resumable fashion to https://transfer.atlassian.com
Python
4
star
29

oss-maven-template

A template for Maven-based projects that should be published to Maven Central.
Java
4
star
30

msteams-jira-server-addon

Official plugin for Jira Server that integrates with Microsoft Teams.
Java
3
star
31

DC-App-monitoring-EAP-dashboards

Grafana dashboards to accompany the DC App monitoring EAP Java agent
2
star
32

artifact-publish-token

Custom Action to retrieve temporary tokens to publish artifacts
JavaScript
2
star
33

Compass-Orb

An orb for reporting the status of CircleCI builds and deployments to Atlassian Compass
Shell
2
star
34

figma-for-jira

A Connect app for integrating Figma designs into Jira.
TypeScript
2
star
35

jql-kotlin

Build JQL query as Kotlin code which resembles the query it generates.
Kotlin
1
star
36

gradle-release

Gradle plugin which can release an open source Atlassian lib to Maven.
Kotlin
1
star
37

tectonics

Atlaskit, but with Web Components
TypeScript
1
star
38

oss-gradle-template

A template for Atlassian open source projects based on Gradle
Java
1
star
39

kotlin-url-dsl

Build URL as Kotlin code which resembles the URL it generates.
Kotlin
1
star