npm package: @rainbow_deer/layout
Description: React component.
Author: guojianqiang
Latest version: 0.1.6

# `Layout`

## install

```shell script
npm install rainbow_deer/layout
or
yarn add rainbow_deer/layout
```

## usage

### basic

```tsx
import React from 'react'
import Layout from '@rainbow_deer/layout'
import { createHashHistory } from 'history'
import { HomeOutlined } from '@ant-design/icons'

export default () => {
  const routeItems = [
    {
      path: '/home',
      exact: true,
      meta: {
        tabFixed: true,
        isCache: true,
        icon: 'home',
        name: '首页',
      },
      component: () => <div>home</div>,
    },
    {
      path: '/test',
      exact: true,
      meta: {
        isCache: true,
        icon: 'success',
        name: '测试页',
      },
      component: () => <div>test</div>,
    },
  ]

  return (
    <div className='storybook__container'>
      <Layout
        proName='admin-demo'
        // aliveControl={aliveControl}
        routeItems={routeItems}
        history={createHashHistory()}
        username={'测试'}
        onClickDrop={() => {
          console.log('退出')
        }}
      >
        <div>layout</div>
      </Layout>
    </div>
  )
}
```

### cache route

```shell
yarn add react-router-cache-route
or
npm install react-router-cache-route
```

```tsx
import React from 'react'
import { Router, Redirect, Route } from 'react-router-dom'
import Layout from '@rainbow_deer/layout'
import { createHashHistory } from 'history'
import {
  CacheRoute,
  CacheSwitch,
  refreshByCacheKey,
  dropByCacheKey,
} from 'react-router-cache-route'

const aliveControl = {
  dropByCacheKey,
  refreshByCacheKey,
}
const Counter = () => {
  let [count, setCount] = React.useState(0)
  const handleAdd = () => setCount(++count)
  return (
    <>
      <button onClick={handleAdd}>+</button>
      {count}
    </>
  )
}

export default () => {
  const history = createHashHistory()
  const routeItems = [
    {
      path: '/home',
      exact: true,
      meta: {
        tabFixed: true,
        isCache: true,
        icon: 'home',
        name: '首页',
      },
      component: () => <div>home</div>,
    },
    {
      path: '/test',
      exact: true,
      meta: {
        isCache: true,
        icon: 'success',
        name: '测试页',
      },
      component: Counter,
    },
  ]
  const renderRoutes = () => {
    let routes = []
    const routeMap = (arr) => {
      arr.forEach((route) => {
        if (!route.meta.hidden) {
          routes.push(
            <CacheRoute // 替换Route => CacheRoute
              when={() => !!route.meta.isCache} // 是否缓存
              cacheKey={route.path} // 缓存key
              key={route.path}
              exact={route.exact}
              path={route.path}
              component={route.component}
            />
          )
        }
        if (route.routes && route.routes.length) routeMap(route.routes)
      })
    }
    routeMap(routeItems)
    return routes
  }
  return (
    <Router history={history}>
      <CacheSwitch>
        <Layout
          logo={
            'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAACCCAMAAACww5CIAAACf1BMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8YkP8AAAACCxMamv/6+voaGhoXi/YYjv8aoP8cq/8dr/8bo/8cqP8bpv8Ykv8drv8BAwUcrP8Zlf8Xjf/s7OzLy8scp/8anP8ZmP/d3d0BBArg4ODT09O7u7sEGCsKCgoanf8YlP/8/Pz09PTIyMgMTIV1dXUGKEVEREQ0NDQODg4GBgYdsv8dsf8Zl//m5uYVgOXj4+MWgtfW1tYTc87BwcERbLWzs7Ovr6+np6cQX6OgoKCTk5MMSXlwcHBra2tiYmIVFRUetf/39/fp6ekWhOkXi+QVfNvY2NjPz88TdcUSb7u6urq3t7cPYK0NUJGQkJCLi4ttbW0JO2cINFtVVVVRUVEHMFEHLEs6OjoEHDEiIiIcHBwXj/vx8fEWh+4Sb8gRbL+rq6upqakOVZiWlpaJiYmGhoYMSIF9fX15eXkKPnQLRHJMTExHR0c9PT0FHzkqKiomJiYEFyUBBw8bovfu7u4Wht4UedsUeMrFxcW9vb0RZrOkpKSampoPXZqAgIALQmtlZWUJOGJZWVkIMFcFIUExMTEwMDAtLS0DEh8Zl/v4+PgXj/QWhvEWhvAYku8Yju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902yNgoriWwCeqw1fSL+J2VkWNBF8vckr6mPQ3ZcjtkVBA/3z4Ju6Bs5ANzck2BQFpUMTxlVZQ4ege95vUxRUHoPOe5s01OWBbryf2hEFDX4Fc4Vs4gaYZ3ZEQeXBJPgMcFPnwYzJVmeE6jGsGCNAE/rAlPIBamkMQv9YCLpzxJRjYMr5BLXyg5EvgTlKTOoEkw2LUct6dTz4ojqCNO04mMm4ZE150mhMuQ+jHppwAUxqUM5QK9qkPLIE5jhpygkvmHJYiW45FaL8IwmdZy9pUtc2MK9HtvgloZngJyMVp3tJ846ASb7Q1NYrg1JN+ukDs4e05LwHTO5bUKG0tRBEeXAKzJ3rpEXdB8C9fBIWKW0hhOBIBdy2K6R11zvALY6EFYE21yHF4OdKEkz7ObIlXXvAhV4OquoApaYbpCo9qayA29lLturibhimSgOSFjG1ILRwYnwShn09xArnT8PwdnHML6n+hl+2gD8Wjj+rLMOwq49Y5dZpVKUWS++VcCwdCdT5/Uhck5SH45VpVO3qJFbq2Y5Vvly2VBgQY5KqKWI6HY+n06KiqVJMSQyP/37wB6v29xGrnThyEDWh5dyr+fJscbQw/OjRcGG0OFvO3n+QSqKm7exlYgsvNgolkyFs1HGV2OQgTGsjNjnVBtO8Owj3nwbhgWnttgWxy2PaoWaC+AuAXqWYKHupMgAAAABJRU5ErkJggg=='
          }
          proName={'admin'}
          routeItems={routeItems}
          username={'admin'}
          history={history}
          aliveControl={aliveControl}
        >
          {renderRoutes()}
        </Layout>
      </CacheSwitch>
    </Router>
  )
}
```