【问题标题】:React-Router <Link> component doesn't get activeStyle applied, after connecting redux-store连接 redux-store 后,React-Router <Link> 组件未应用 activeStyle
【发布时间】:2016-11-11 05:16:33
【问题描述】:

我最近开始学习 React / Redux,现在我正在尝试构建一个小型单页应用程序。

最近我遇到了一个我不理解且无法解决的问题。

让我给你看代码:

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import { Router, Route, IndexRoute, hashHistory } from 'react-router'
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

import App from './components/App'
import Home from './components/Home'
import About from './components/About'

const reducer = (state = {}, action) => {
  return state
}

const store = createStore(
  combineReducers({
    reducer,
    routing: routerReducer
  })
)

const history = syncHistoryWithStore(hashHistory, store)

ReactDOM.render((
  <Provider store={store}>
    <Router history={history}>
      <Route path='/' component={App}>
        <IndexRoute component={Home}/>

        <Route path='about' component={About}/>
      </Route>
    </Router>
  </Provider>
), document.getElementById('root'))

基本上这是来自 to react-router-redux GitHub 页面的示例(向下滚动到 Tutorial 部分) - 唯一的区别是,我使用的是 hashHistory .

App.js

import React, { Component, PropTypes } from 'react'

import Navigation from './Navigation'

export default class App extends Component {
  render() {
    return (
      <div>
        <Navigation/>

        {this.props.children}
      </div>
    )
  }
}

App.propTypes = {
  children: PropTypes.node
}

导航.js

import React, { Component } from 'react'
import { Link, IndexLink } from 'react-router'

export default class Navigation extends Component {
  render() {
    return (
      <ul role='nav'>
        <li><IndexLink to='/' activeStyle={{ color: 'red' }}>Home</IndexLink></li>
        <li><Link to='/about' activeStyle={{ color: 'red' }}>About</Link></li>
      </ul>
    )
  }
}

Home 和 About 组件只是渲染一个

标题来可视化 SPA 的状态。

到目前为止,一切都如您所愿!当用户点击 About 链接时,URL 会相应更改,并且链接会变为红色(因为 activeStyle 属性)。

但是我的问题来了(感谢您仍然阅读本文):

我想通过 react-redux connect() 包装我的 组件。所以这是我的新版本

导航.js

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link, IndexLink } from 'react-router'

class Navigation extends Component {
  render() {
    return (
      <ul role='nav'>
        <li><IndexLink to='/' activeStyle={{ color: 'red' }}>Home</IndexLink></li>
        <li><Link to='/about' activeStyle={{ color: 'red' }}>About</Link></li>
      </ul>
    )
  }
}

const NavigationContainer = connect()(Navigation)

export default NavigationContainer

但是现在 activeStyle 功能似乎被破坏了......当我浏览我的应用程序时,当前活动的链接不再改变它的颜色。

我真的试图解决这个问题几个小时:(非常感谢任何帮助!

【问题讨论】:

    标签: javascript reactjs redux react-router react-router-redux


    【解决方案1】:

    react-redux 的 connect() 阻止 Navigation 以及随后的链接在位置状态更改时重新呈现。

    如果您在 Navigation 的渲染中添加 console.log("Navigation render"),您会看到添加 connect() 函数后,组件不会在位置状态更改时重新渲染。


    有办法避免

    1.way: 为了在位置变化时重新渲染 Navigation comp,您可以添加 prop,类似 location={this.props.location}

    export default class App extends Component {
      render() {
        return (
          <div>
            <Navigation location={this.props.location}/>
    
            {this.props.children}
          </div>
        )
      }
    }
    

    2.way:{pure:false} 添加到connect()。 DOCS LINK

    export default connect(mapStateToProps, actions, null, { pure: false })(Navigation);
    

    3.way:这已在 React Router 3.0.0-alpha.1 和更新版本中得到修复。

    【讨论】:

    • MobX 也有类似的问题。 react-router@3.0.0-alpha.3 似乎工作,而 2.x 没有。
    • 这严重需要用 React-Router 更好地记录。起初人们可能不认为这是他们要纠正的,因为 Redux 的 Connect() 是罪魁祸首。但显然他们对 3.0.0-alpha.x 中的更改做了。直到我从@ultro 偶然发现了这个,我都快疯了。
    • 我寻找这个答案已经很久了!很高兴知道它已在 3 中修复。我正在使用垃圾超时功能来强制更新(),直到我升级。
    猜你喜欢
    • 2017-12-17
    • 2017-06-23
    • 2017-06-03
    • 2020-05-05
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    相关资源
    最近更新 更多