react-router-dom示例讲解(八)——侧边栏

前言:

1、react定义组件有两种方式:

1)、用class即定义类的形式,这种形式我们前面用到好多次了。

class WillMatch extends Component{
  render(){
    return(
      <h1>
        Matched
      </h1>
    )
  }
}
2)、函数定义组件,这种方式是一中简便的定义组件的方式,适用于定义类方式中只有render 方法的简单组件。
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}
2、react写样式的三种方式:

1)、利用className,相当于css中的class。具体样式在style中定义。

    class Square extends React.Component {
      render () {
        return (
          <button className="square">
            {/*TODO*/}
          </button>
        )
      }
    }
  <style>
    .square {
      background: #fff;
      border: 1px solid #999;
      float: left;
      font-size: 24px;
      font-weight: bold;
      line-height: 34px;
      height: 34px;
      margin-right: -1px;
      margin-top: -1px;
      padding: 0;
      text-align: center;
      width: 34px;
    }
  </style>
2)、定一个样式对象,其属性值为样式值,将变量在jsx中解析。
  <script type="text/babel">
    var myStyle = {
      fontSize: 100,
      color: '#ff0000'
    }
    ReactDOM.render(
      <h1 style = {myStyle}>haha</h1>,
      document.getElementById('example')
    );
  </script>
3)、今天我们例子中用的是另外一种,和第二种类似。
<div style={{ flex: 1 }}>
  {
    routes.map((item, index) => {
      return (
        <Route path={item.path} key={index} exact={item.exact} component={item.main} />
      )
    })
  }
</div>
根据不用情况,读者可自行选择。

实现效果:

代码如下:

const routes = [
  {
    path: '/',
    exact: true,
    sidebar: () => <div>home!</div>,
    main: () => <h2>Home</h2>
  },
  {
    path: '/bubblegum',
    sidebar: () => <div>bubblegum!</div>,
    main: () => <h2>Bubblegum</h2>
  },
  {
    path: '/shoelaces',
    sidebar: () => <div>shoelaces!</div>,
    main: () => <h2>shoelaces</h2>
  }
]

class App extends Component{
  render() {
    return(
      <Router>
        <div style={{ display: 'flex' }}>
          <div style={{ width: '200px', backgroundColor: '#cccccc' }}>
            <ul style={{listStyleType: 'none',padding: 0}}>
              <li><Link to='/'>Home</Link></li>
              <li><Link to='/bubblegum'>Bubblegum</Link></li>
              <li><Link to='/shoelaces'>Shoelaces</Link></li>
            </ul>
            {
              routes.map((item, index) => {
                return (
                  <Route path={item.path} key={index} exact={item.exact} component={item.sidebar} />
                )
              })
            }
          </div>
          <div style={{ flex: 1 }}>
            {
              routes.map((item, index) => {
                return (
                  <Route path={item.path} key={index} exact={item.exact} component={item.main} />
                )
              })
            }
          </div>
        </div>
      </Router>
    )
  }
}
Route对应的组件便是用函数定义的方式定义的。

倘若你还不理解请参考我的github上的这个示例:https://github.com/guoqin721/react-router-dom8









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Router是一个用于构建单页面应用程序的库。它提供了一种在React应用程序中管理路由方式React Router DOM是React Router的一个扩展,用于在Web应用程序中进行路由React Router DOM 5和React Router DOM 6之间有几个重要的区别: 1. 安装方式React Router DOM 5使用npm包管理器进行安装,命令为`npm install react-router-dom`。而React Router DOM 6使用yarn进行安装,命令为`yarn add react-router-dom@next`。 2. 路由组件:在React Router DOM 5中,使用`<Route>`组件定义路由。而在React Router DOM 6中,使用`<Route>`组件的替代方案`<Routes>`来定义路由。 3. 路由匹配方式React Router DOM 5使用基于路径的匹配方式来确定哪个路由应该被渲染。而React Router DOM 6引入了新的匹配方式,称为元素匹配(element matching),它可以根据组件的类型来匹配路由。 4. 嵌套路由:在React Router DOM 5中,嵌套路由需要使用嵌套的`<Route>`组件定义。而在React Router DOM 6中,可以使用嵌套的`<Routes>`组件定义嵌套路由。 5. 动态路由:在React Router DOM 5中,可以通过在路径中使用参数来定义动态路由。而在React Router DOM 6中,可以使用`<Route>`组件的新属性`element`来定义动态路由。 6. 错误处理:React Router DOM 5使用`<Switch>`组件来处理路由匹配错误。而React Router DOM 6使用`<Routes>`组件的新属性`fallback`来处理路由匹配错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值