javascript – 为什么React-router v4不起作用(更改网址但不提取内容)?

我有服务器端React / Redux / Express应用程序.
React-router v4为使用Switch的服务器应用程序提供解决方案,我需要使用一些东西来改变我的NavBar组件的位置

应用

import React,{ Component } from 'react'

import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

import { Switch,Route,Redirect,Link} from 'react-router-dom'
import FirstPage from './FirstPage'
import Test from './Test'
import LoginPage from './login/LoginPage'
import NoMatch from '../components/NoMatch'
import NavBar from '../components/NavBar'

import * as loginActions from '../actions/login'

import 'bootstrap/dist/css/bootstrap.css';

class App extends Component {

  render(){
    return (
      irstPage/>)}/>
              irst" render={loginRedirect(irstPage/>)}/>
              irst'}>FirstetoProps = state => ({
    login: state.login,error: state.error,isLoading: state.isLoading,})

const mapdispatchToProps = dispatch => ({
    loginActions: bindActionCreators(loginActions,dispatch)
})

export default connect(
    mapStatetoProps,mapdispatchToProps
)(App)

需要改变
的NavBar

import React from 'react'
import { Link,NavLink } from 'react-router-dom'

import classnames from 'classnames'

const NavBar =()=> {
    return (
        

如果我从浏览器URL手动导航它的工作就好了,但如果我点击链接或NavLink网址更新但不是应用程序切换.当loginRedirect到/ login它没有出现并且需要刷新页面时我也遇到了问题(可能这两个是相关的).
如何解决这个问题?

最佳答案
我认为这里的问题是使用redux ..因为只要道具没有改变,它就会阻止重新渲染组件,

This is because connect() implements shouldComponentUpdate by default,assuming that your component will produce the same results given the same props and state.

The best solution to this is to make sure that your components are pure and pass any external state to them via props. This will ensure that your views do not re-render unless they actually need to re-render and will greatly speed up your application.

If that’s not practical for whatever reason (for example,if you’re using a library that depends heavily on React context),you may pass the pure: false option to connect():

function mapStatetoProps(state) {
  return { todos: state.todos }
}

export default connect(mapStatetoProps,null,{
  pure: false
})(TodoApp)

这里有更多解释的链接

react-redux troubleshooting section

react-router DOCS

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...