Commit a614346b by Wee

docs(README): update docs

parent c8adb195
An enhanced version of react-router-v4 **Route** Component that keeps route component alive on unmatched path and recover it completely on match path.
An enhanced version of **react-router-v4 Route** Component that keeps route component alive on unmatched path and restore it completely on match path.
## Document
......@@ -8,7 +8,7 @@ An enhanced version of react-router-v4 **Route** Component that keeps route comp
### codeSandbox
You can experience react-live-route on codeSandbox.
You can experience and review the source code on codeSandbox.
[![Edit react-live-route-demo-1](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/yj9j33pw4j)
......@@ -24,35 +24,43 @@ You also can scan the QR code of the demo above to experience it on mobile devic
npm install react-live-route --save-dev
```
or
```bash
yarn add react-live-route --dev
```
## About
It can keeps component of route hidden (alive) instead of unmout when the path is not match. There are a few APIs provided to control the life cycle of component.
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:
We have a list page, click on the items in the list page will enter the details page, when entering the details page, the list page will be hidden, when returning to the list page, the list page will revert to the last time you left.
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-4, all passed the react-router-v4 unit tests.
- 📦 Completely restored the last time you left the page (scroll position included).
- 🎯 Minimally invasive, all you need to do is importing a LiveRoute.
- ✌️ Blazing easy API.
- ✅ 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.
## ⚠️ Caveat
## Caveat ⚠️
- LiveRoute **SHOULD NOT** be wrapped by `Switch` directly, because `Switch` only render the first matched component so that LiveRoute may be directly skipped.
- If a route uses LiveRoute and the parent route of the current route is unmounted, then it will be unmounted whether or not livePath is match. 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.
- LiveRoute **SHOULD NOT** be wrapped by `Switch` directly, cause `Switch` only render the first matched child element so that LiveRoute may be skipped directly. You can move LiveRoute from `Switch` to the outside.
- 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.
## API
## Usage
### livePath
`livePath` is the path of the page that needs to be hidden instead of unmounted. 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` 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.
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 entering `/user/:id`, and it will be unmounted normally when entering other location.
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.
```jsx
import LiveRoute from 'react-live-route'
......@@ -62,11 +70,11 @@ import LiveRoute from 'react-live-route'
### alwaysLive
`alwaysLive` is just like `livePath`. It will be re-render when goes back to match location. But the difference is the component will not be unmount on **any other location** after the first mount.
`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 be re-render when match again.
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.
```jsx
import LiveRoute from 'react-live-route'
......@@ -76,4 +84,4 @@ import LiveRoute from 'react-live-route'
## Licence
MIT
\ No newline at end of file
MIT
react-router-v4 Route 组件的加强版,可以使路由在路径不匹配时隐藏而不卸载
**react-router-v4 Route** 组件的加强版,可以保持路由的组件在路径不匹配时隐藏而不卸载,而在回到返回到匹配路径时完全恢复离开页面时的样子
## 文档
[English](./docs/README-zh.md)
## Demo
### codeSandbox
可以在 codeSandbox 上体验并查看源码。
[![Edit react-live-route-demo-1](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/yj9j33pw4j)
### QR code
你也可以使用移动端设备扫描二维码来体验。
![qr](./qr.png)
## 安装
```bash
npm install react-live-route --save-dev
```
或者
```bash
yarn add react-live-route --dev.
```
## 关于
可以让 Route 在特定路径不匹配的时候隐藏(live)而不被卸载,react-live-route 提供了几种保持组件的 API 用来控制组件的存活情况。
可以让 Route 在特定路径不匹配的时候隐藏而不被卸载路由的组件,并在路径返回时完全恢复离开路径时的模样。react-live-route 提供了几种 API 来控制组件的隐藏状况。
举个例子:
一个列表页,点击列表页中的项目会进入详情页,当进入详情页时,列表页会隐藏,当返回列表页时,列表页会恢复到上一次离开时的模样。
一个列表页,点击列表页中的项目会进入详情页,当进入详情页时,列表页会在处于详情页中时一直被隐藏,当返回列表页时,列表页会恢复到上一次离开时的模样。
## 特点
- ✅ 完全兼容 react-router-v4,通过了 react-router-v4 的单元测试。
- 📦 完全恢复上一次离开页面时的模样(包括滚动位置)。
- 🎯 侵入性极小,只需引入一个 LiveRoute。
- 🎯 完全恢复上一次离开页面时的模样(包括滚动位置)。
- 🔒 侵入性极小,只需引入一个 LiveRoute。
- ✌️ 简单易懂的 API。
## API
## 须知 ⚠️
- LiveRoute **不能**直接嵌套在 `Switch` 组件中,因为 `Switch` 会只渲染第一个路径匹配的子元素所以 LiveRoute 可能会被直接跳过,你可以将 LiveRoute 从 `Switch` 中移出。
- 如果一个 LiveRoute 的路由在当前路径上被卸载了,那么它也将会被卸载。这是由 React 自顶向下的设计理念决定的,你可以使用 LiveRoute 来声明父路由或者不要嵌套路由。
## 用法
### livePath
`livePath` 为需要隐藏的页面的路径,具体规则与 react-router 中的 Route 的 `path` props 一样,使用 `component``render` 来渲染路由对应的组件。
LiveRoute 会在从 `livePaht` 匹配的路径返回 `path` 匹配的路径时冲渲染,在进入其他不匹配的路径时会直接卸载。
例如:
List 的路由会在 `/list` 下正常渲染,当进入 `/user/:id` 时会隐藏,当进入这两者以外的页面时会正常卸载。
......@@ -43,7 +70,7 @@ import LiveRoute from 'react-live-route'
### alwaysLive
`alwaysLive` 会在路由对应的组件完成第一次初始化后,在其他不匹配的页面下阻止页面的卸载。使用 `component``render` 来渲染路由对应的组件。
`alwaysLive` `livePath` 差不都,区别是路由的组件会在第一次 mount 后在**其他任何路径**都不会再被卸载
例如:Modal 页面在第一次正常渲染之后,进入路径不匹配的页面时隐藏,而在 Modal 路径匹配时更新渲染。
......@@ -53,9 +80,7 @@ import LiveRoute from 'react-live-route'
<LiveRoute path="/list" alwaysLive={true} component={Modal}/>
```
### forceUnmount
🚸 WIP
### forceUnmount (WIP) 🚧
可以传入一个函数,当这个函数返回值为真时,可以强制路由对应的组件卸载,对应的函数签名为
......@@ -71,10 +96,6 @@ import LiveRoute from 'react-live-route'
<LiveRoute path="/list" livePath="/user/:id" component={List} forceUnmount={(props, params)=> params.id === 27}/>
```
## ⚠️ 注意
- 如果某路由使用了 LiveRoute,如果当前路由的上层路由被卸载了,那么不管这个 LiveRoute 是不是当前匹配 livePath,都将被卸载,这是由 React 自顶向下的设计原则决定的,你可以使用 LiveRoute 声明父路由来解决这个问题。
## Licence
MIT
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment