All Projects β†’ fi3ework β†’ React Live Route

fi3ework / React Live Route

Licence: mit
πŸ“Œ An enhanced react-router-v4/5 Route that keeps route alive.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Live Route

React Redux Graphql Apollo Bootstrap Webpack Starter
react js + redux + graphQL + Apollo + react router + hot reload + devTools + bootstrap + webpack starter
Stars: ✭ 127 (-38.65%)
Mutual labels:  router, react-router, react-router-v4
React Redux Antdesign Webpack Starter
react + redux + ant design + react-router 4 + webpack 4 starter
Stars: ✭ 44 (-78.74%)
Mutual labels:  router, react-router, react-router-v4
React Dashboard
πŸ”₯React Dashboard - isomorphic admin dashboard template (React.js, Bootstrap, Node.js, GraphQL, React Router, Babel, Webpack, Browsersync) πŸ”₯
Stars: ✭ 1,268 (+512.56%)
Mutual labels:  react-router, react-router-v4
Spring Security React Ant Design Polls App
Full Stack Polls App built using Spring Boot, Spring Security, JWT, React, and Ant Design
Stars: ✭ 1,336 (+545.41%)
Mutual labels:  react-router, react-router-v4
Universal React Redux
🧐 A sensible universal starter kit for React + Redux
Stars: ✭ 112 (-45.89%)
Mutual labels:  react-router, react-router-v4
Create React App Redux
React Router, Redux, Redux Thunk & Create React App boilerplate
Stars: ✭ 885 (+327.54%)
Mutual labels:  react-router, react-router-v4
Simple Universal React Redux
The simplest possible Async Universal React & Redux Boilerplate app, that works on both Mac and Windows
Stars: ✭ 58 (-71.98%)
Mutual labels:  react-router, react-router-v4
Road Beyond React App
🌈 The Road beyond React - Thing you can use after learning plain React.js
Stars: ✭ 108 (-47.83%)
Mutual labels:  react-router, react-router-v4
React Native Simple Router
A community maintained router component for React Native
Stars: ✭ 266 (+28.5%)
Mutual labels:  router, react-router
React Redux Bootstrap Webpack Starter
React 16.9 + Typescript + React-Router 4 + Redux + Bootstrap 4 + Hot Reload + redux-devtools-extension + Webpack 4 + styled-components STARTER
Stars: ✭ 133 (-35.75%)
Mutual labels:  router, react-router-v4
Redux React Session
πŸ”‘ Simple Session API storage for Redux and React
Stars: ✭ 140 (-32.37%)
Mutual labels:  react-router, react-router-v4
Favesound Mobx
🎢 A SoundCloud Client in React + MobX running in production. Live Demo and Source Code to explore React + MobX. Refactored from favesound-redux
Stars: ✭ 532 (+157%)
Mutual labels:  react-router, react-router-v4
React Router Page Transition
Highly customizable page transition component for your React Router
Stars: ✭ 531 (+156.52%)
Mutual labels:  router, react-router
React Router Util
Useful components and utilities for working with React Router
Stars: ✭ 320 (+54.59%)
Mutual labels:  router, react-router
Redux First History
πŸŽ‰ Redux First History - Redux history binding support react-router - @reach/router - wouter
Stars: ✭ 163 (-21.26%)
Mutual labels:  router, react-router
React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-50.24%)
Mutual labels:  react-router, react-router-v4
yarr
A React router library enabling the render-as-you-fetch concurrent UI pattern.
Stars: ✭ 97 (-53.14%)
Mutual labels:  router, react-router
froute
Type safe and flexible router for React
Stars: ✭ 31 (-85.02%)
Mutual labels:  router, react-router
Favesound Redux
🎢 A SoundCloud Client in React + Redux running in production. Live Demo and Source Code to explore React + Redux as a beginner.
Stars: ✭ 1,586 (+666.18%)
Mutual labels:  react-router, react-router-v4
V2 Universal Js Hmr Ssr React Redux
⚑ (V2) Universal JS - Server Side Rendering, Code Splitting and Hot Module Reloading ⚑
Stars: ✭ 147 (-28.99%)
Mutual labels:  react-router, react-router-v4

react-live-route

An enhanced version of react-router-v4 Route Component that keeps route component alive on unmatched path and restore it completely on match path.

NPM Version Build Status Coverage Status

Document

δΈ­ζ–‡

Feedback

Feel free to open an issue to ask for help or have a discussion. If there is a detailed code problem help is needed, please fork this minimal demo to provide a reproduce scenario, or your issue might be closed directly due to lack of information.

Demo

codeSandbox

You can experience and review the source code on codeSandbox.

Edit react-live-route-demo-1

QR code

You also can scan the QR code of the demo above to experience it on mobile device.

qr

Install

npm install react-live-route

or

yarn add react-live-route

About

It will hide component of route instead of unmout it when the path is not match and restore it when come back. There are a few APIs provided to control the hidden condition of component.

Example:

There is a item list page, click on the items on this page will enter the item detail page. When entering the detail page, item list page will be hidden and it will keep hidden when you are on item detail page. Once back to the list page, the list page will be restored to the last state of leaving.

Features

  • βœ… Fully compatible with react-router-v4, all passed the react-router-v4 unit tests.
  • 🎯 Completely restored to the state of the last time you left (scroll position included).
  • πŸ”’ Minimally invasive, all you need to do is import LiveRoute.
  • ✌️ Super easy API.

Hint

  • Using with , see [Use in <Switch>](#Use in <Switch>) for detail.
  • Using with code splitting, see ensureDidMount for detail.
  • If LiveRoute's parent route is unmounted on current location, then it will also be unmounted . This is determined by the top-down design principle of React. You can use LiveRoute to declares a parent route to solve this problem or stop nestting the router.
  • In some cases the DOM of LiveRoute will be modified directly and the scroll position will not change when navigation. This is not a problem with react-live-route. You can scroll the screen to the top manually and you may get some help from this article from react-router. By the way, if the scroll position will be restored by LiveRoute, it will come up after the scroll operation in componet of LiveRoute due to the render order of React.

Usage

1. Enhance Route with withRouter

The class imported from react-live-route must be wrapped by withRouter to touch the property of context to work as expected.

import NotLiveRoute from 'react-live-route'
import { withRouter } from 'react-router-dom'

const LiveRoute = withRouter(NotLiveRoute)

2. Props of LiveRoute

livePath: (string | string[])

livePath is the path you want to hide the component instead of unmount it. The specific rules of livePath are the same as path props of Route in react-router-v4. You still can use component or render props to render a component.

livePath also can accept an array of string above since 1.2.0. livePath is matched if any string in the array is matched.

LiveRoute will re-render when it come back from a path matching location from the livePath matching location. It will unmount on other unmatched locations.

Example:

The route of List will be rendered normally under /list, and it will be hidden when location change to /user/:id, and it will be unmounted normally when entering other locations.

import NotLiveRoute from 'react-live-route'
import { withRouter } from 'react-router-dom'

const LiveRoute = withRouter(NotLiveRoute)

<LiveRoute path="/list" livePath="/user/:id" component={List} />

alwaysLive: boolean

alwaysLive is just like livePath. The difference is the component will not be unmount on any other location after the it's first mount.

Example:

After the first mount on match location, the Modal page will be hidden when the path is not matched, and will re-render when path match again.

import NotLiveRoute from 'react-live-route'
import { withRouter } from 'react-router-dom'

const LiveRoute = withRouter(NotLiveRoute)

<LiveRoute path="/list" alwaysLive={true} component={Modal} />

onHide: (location, match, history, livePath, alwaysLive) => void

This hook will be triggered when LiveRoute will hide in componentWillReceiveProps stage (so it happens before re-render).

Example of usage is below.

onReappear: (location, match, history, livePath, alwaysLive) => void

This hook will be triggered when LiveRoute will reappear from hide in componentWillReceiveProps stage (so it happens before re-render).

import NotLiveRoute from 'react-live-route'
import { withRouter } from 'react-router-dom'

const LiveRoute = withRouter(NotLiveRoute)

<LiveRoute
  path="/items"
  component={List}
  livePath="/item/:id"
  name="items"
  onHide={(location, match, livePath, alwaysLive) => {
    console.log('[on hide]')
  }}
  onReappear={(location, match, livePath, alwaysLive) => {
    console.log('[on reappear]')
    console.log(routeState)
  }}
/>

forceUnmount: (location, match, history, livePath, alwaysLive) => boolean

forceUnmount is funtion that return a boolean value to decide weather to forceUnmount the LiveRoute no matter it is matched or should be kept lived.

For example: when the user id equals to 27, List page will be force unmounted while routing on other value of id will be kept.

import NotLiveRoute from 'react-live-route'
import { withRouter } from 'react-router-dom'

const LiveRoute = withRouter(NotLiveRoute)

<LiveRoute path="/list" livePath="/user/:id" component={List} forceUnmount={(location, match)=> match.params.id === 27}/>

ensureDidMount (code-splitting)

ensureDidMount is useful when using a route with react-loadable. Cause react-loadable will load the route component asynchronous. So the route component must give a hint to help react-live-route know when the real component is loaded so the DOM could be got.

const LoadableItems = Loadable({
  loader: () => import("./list"),
  loading: () => () => <p>xxx</p>
})

List item:

  componentDidMount() {
    this.props.ensureDidMount()
  }

TIPS

Use in <Switch>

There's some stuff extra to do when Using with <Switch>. If you want makes /items pathname alive, you must put a placeholder route where it should be but renders nothing. And put LiveRoute out of Switch.

(You have to do this because keep-alive break the origin <Switch> or React philosophy so we have use a placeholder route to keep the philosophy and show the real content out of .)

function App() {
  return (
    <div className="App">
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/item/:id" component={Detail} />
        <Route path="/about" component={About} />
        <Route path="/items" /> // placeholder(required)
        <Route path="*" render={NotFound} />
      </Switch>
      <LiveRoute
        path="/items"
        component={List}
        livePath="/item/:id"
        name="items"
        onHide={routeState => {
          console.log("[on hide]");
          console.log(routeState);
        }}
        onReappear={routeState => {
          console.log("[on reappear]");
          console.log(routeState);
        }}
      />
      <Bar />
    </div>
  );
}

Licence

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].