npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-view

v1.0.7

Published

this is a component for react-router-dom

Downloads

10

Readme

ReactRouterView

RouterView是react-router-dom 4.0的一个组件,该组件实现了用对象配置路由,可以在一个js文件中配置所有路由,在需要显示路由视图的地方直接使用RouterView组件代替,该组件有一个routes属性,routes是一个数组,即路由的配置,相对于react-router4.0官方的使用方法,大大简化了项目中的代码,使路由的配置更清晰、直观。

使用方法:

安装

   npm i react-router-view --save

使用示例

示例1:在BrowserRouter组件中直接使用RouterView组件,把路由的配置通过routes属性传到组件中

   import React, {Component} from 'react';
   import {BrowserRouter} from 'react-router-dom';
   import RouterView from "react-router-view"; // 导入react-router-view
   import getRoutes from "./common/routerConfig";
   const routes = getRoutes()//路由的配置,

   function App(props) {
      return (
         <BrowserRouter>
            <RouterView routes={routes}></RouterView>
         </BrowserRouter>
      )
   }
   export default App;

示例2:比如 "/" 匹配到的组件是Layout, "/home"是"/"子路由,"/home"匹配到的组件Home要在Layout中渲染,如下, 在需要渲染Home组件的地方使用RouterView组件,在Layout组件的props中可以拿到 "/" 的子路由childRoutes,把子路由的数据通过routes属性传到RouterView组件中,当跳转到 "/home"的时候,RouterView组件的位置就会显示"/home"匹配到的Home组件,多层嵌套的路由也是如此

   import React from 'react';
   import RouterView from "react-router-view";

   function Layout(props) {
      return (
         <div>首页</div>
         <div>
            <RouterView routes={this.props.childRoutes}></RouterView>
         </div>
      )
   }
   export default Layout;

示例3:路由配置规则如下

   import {asyncComponent} from "./asyncComponent";
   const Layout = asyncComponent(() => import ("@/layout"));
   const Home = asyncComponent(() => import ("@/routes/home"));
   const List = asyncComponent(() => import ("@/routes/list"));
   const Details = asyncComponent(() => import ("@/routes/details"));

   export default function () {
      const routes = [
         {
            path: "/", // url
            component: Layout, //url匹配的组件
            redirect: "/home", // 把 "/" 重定向到 "/home"
            childRoutes: [ // "/"的子路由
               {
                  path: "/home",
                  component: Home,
               }, {
                  path: "/list",
                  component: List,
                  childRoutes: [ // "/list"的子路由
                     {
                        path: "/list/:id", // 动态路由
                        component: Details
                     }
                  ]
               }
            ]
         }
      ]
      return routes;
   }

完整示例