Lucifier129 / react-lite

An implementation of React v15.x that optimizes for small script size
MIT License
1.73k stars 100 forks source link

菜单高亮只会初始化一次,之后的点击不会生效 #122

Closed henryzp closed 6 years ago

henryzp commented 6 years ago

用create-react-app创建一个应用,然后eject弹出一下,alias配置react-lite。。

在package.json中加入:

"react-router-dom": "^4.0.0"

将App.js的内容改为下面的的代码:

import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

const CustomLinkExample = () => (
  <Router>
    <div>
      <OldSchoolMenuLink activeOnlyWhenExact={true} to="/" label="首页"/>
      <OldSchoolMenuLink to="/about" label="关于"/>
      <hr/>
      <Route exact path="/" component={Home}/>
      <Route path="/about" component={About}/>
    </div>
  </Router>
)

const OldSchoolMenuLink = ({ label, to, activeOnlyWhenExact }) => (
  <Route path={to} exact={activeOnlyWhenExact} children={({ match }) => (
    <div className={match ? 'active' : ''}>
      {match ? '> ' : ''}<Link to={to}>{label}</Link>
    </div>
  )}/>
)

const Home = () => (
  <div>
    <h2>首页</h2>
  </div>
)

const About = () => (
  <div>
    <h2>关于</h2>
  </div>
)

export default CustomLinkExample

效果截图: image

点击首页,首页不会高亮。

使用react是正常的。