3

我最近开始学习 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

应用程序.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 组件只是渲染一个 <h1> 标题来可视化 SPA 的状态。

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

但是我的问题来了(感谢您仍然阅读此顺便说一句):

我想通过react-redux connect() 包装我的 <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

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

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

4

1 回答 1

9

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

如果添加console.log("Navigation render")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()。 文档链接

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

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

于 2016-07-09T07:05:34.380 回答