简体   繁体   English

反应-路由器 <Link> 连接redux-store后,组件未应用activeStyle

[英]React-Router <Link> component doesn't get activeStyle applied, after connecting redux-store

I recently started learning React / Redux and right now I'm trying to build a small single page application. 我最近开始学习React / Redux,现在我正在尝试构建一个小的单页面应用程序。

Lately I ran into an issue that I did't understand and couldn't fix. 最近我遇到了一个我不理解但无法修复的问题。

Let me show you the code: 我来告诉你代码:

index.js 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'))

Basically this is the example from the to react-router-redux GitHub page (scroll down to the section Tutorial ) - the only difference is, that I am using a hashHistory . 基本上这是来自react-router-redux GitHub页面的示例(向下滚动到教程部分) - 唯一的区别是,我使用的是hashHistory

App.js 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
}

Navigation.js Navigation.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>
    )
  }
}

The Home and About components are just rendering a <h1> Headline to visualize the state of the SPA. Home和About组件只是呈现<h1>标题以可视化SPA的状态。

So far, everything works fine as you would expect it! 到目前为止,一切正常,正如您所期望的那样! When the user clicks the About link, the URL changes accordingly and the link turns red (because of the activeStyle property). 当用户单击“ 关于”链接时,URL会相应更改,并且链接将变为红色(因为activeStyle属性)。

But here comes my problem (thanks for still reading this btw): 但是这里出现了我的问题(感谢仍然阅读这个顺便说一句):

I want to wrap my <Navigation> component via react-redux connect(). 我想通过react-redux connect()包装我的<Navigation>组件。 So this is my new version 所以这是我的新版本

Navigation.js Navigation.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

But now the activeStyle feature seems to be broken... When I navigate through my app, the currently active link does not change its color anymore. 但现在activeStyle功能似乎已被破坏......当我浏览我的应用程序时,当前活动的链接不再改变其颜色。

I really tried to solve this issue for hours :( Any help is very appreciated! 我真的试图解决这个问题几个小时:(任何帮助非常感谢!

react-redux's connect() preventing Navigation , and subsequently links, from rerendering on a location state change. react-redux的connect()阻止导航和随后的链接重新发布位置状态变化。

If you add console.log("Navigation render") in Navigation's render , you will see that after you add connect() func, component doesnt re-render on a location state change. 如果在Navigation的渲染中添加console.log("Navigation render") ,您将看到在添加connect()func之后,组件不会在位置状态更改时重新渲染。


There are ways to avoid it 有办法避免它

1.way: in order to re-render Navigation comp on location change, you can add prop , something like that location={this.props.location} 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: add {pure:false} to connect(). 2.way:{pure:false}添加到connect()。 DOCS LINK DOCS LINK

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

3.way: This is fixed in React Router 3.0.0-alpha.1 and newer. 3.way:这在React Router 3.0.0-alpha.1和更新版本中得到修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM