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

前端之家收集整理的这篇文章主要介绍了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 (
      

需要改变
的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

猜你在找的JavaScript相关文章