React Router v4 with multiple layouts

前端 未结 12 1613
旧时难觅i
旧时难觅i 2020-12-13 00:08

I\'d like to render some of my routes within my public layout, and some other routes within my private layout, is there a clean way to do this?

Example that obviousl

12条回答
  •  爱一瞬间的悲伤
    2020-12-13 00:44

    The idea is the same as that of Zaptree's but using es6 syntax and added checks so it can be used in place of react-router's Route component

    Create a new component, say /src/components/Route/index.js:

    import React, {Component} from 'react'
    import PropTypes from 'prop-types'
    import {Route as ReactRoute} from 'react-router'
    
    class Route extends Component {
      static propTypes = {
        component: PropTypes.func.isRequired,
        layout: PropTypes.func,
        path: PropTypes.string,
        exact: PropTypes.bool
      }
    
      render = () => {
        const {component, layout, path, exact} = this.props
        let routeComponent = props => React.createElement(component, props)
    
        if (layout) {
          routeComponent = props =>
            React.createElement(layout, props, React.createElement(component, props))
        }
    
        return 
      }
    }
    
    export default Route
    

    Use the created Route component:

    import Route from 'components/Route/'
    ...
    
    
      
        
        
        
      
    
    

提交回复
热议问题