Javascript 反应路由器4和props.history.push

Javascript 反应路由器4和props.history.push,javascript,reactjs,react-router,react-redux,mern,Javascript,Reactjs,React Router,React Redux,Mern,有件事让我发疯,我需要你的帮助。基本上,当用户单击“我的配置文件”时,我想重定向到用户的配置文件。为此,我使用以下代码段 viewProfile = () => { console.log('My USER ID: ', this.props.userId); this.props.history.push(`/profile/${this.props.userId}`); } 这应该行得通。但是,尽管在单击“我的个人资料”时URL会更改为正确的URL,但页面不会显示。它

有件事让我发疯,我需要你的帮助。基本上,当用户单击“我的配置文件”时,我想重定向到用户的配置文件。为此,我使用以下代码段

viewProfile = () => {
    console.log('My USER ID: ', this.props.userId);
    this.props.history.push(`/profile/${this.props.userId}`);
}
这应该行得通。但是,尽管在单击“我的个人资料”时URL会更改为正确的URL,但页面不会显示。它只是停留在原来的页面上

在谷歌搜索了一段时间后,我知道这与redux有关。。。然而,我找不到解决办法。(this.props.userId来自布局组件,而布局组件又从redux应用商店获取)

这是我的密码:

//反应
从“React”导入React,{Component};
从“react router dom”导入{withRouter};
//重演
从'react redux'导入{connect};
将*作为操作从“../../../store/actions/”导入;
//容器和组件
…//更多的进口
常量样式=//样式
类导航栏扩展组件{
handleAuthentication=()=>{
如果(此.props.isAuthenticated){
this.props.history.push('/logout');
}否则{
this.props.history.push('/login');
}
};
视图配置文件=()=>{
log('My USER ID:',this.props.userId);
this.props.history.push(`/profile/${this.props.userId}`);
};
render(){
让按钮=(
{this.props.isAuthenticated(
):null}
);
让itemSelectField=null;
if(this.props.location.pathname==='/items'){
itemSelectField=(
this.props.filterItemsByTagName(标记)}
/>
);
}
设bar=null;
如果(
this.props.location.pathname!=='/login'&&
this.props.location.pathname!=='/register'
) {
杆=(
);
}
返回{bar};
}
}
const mapDispatchToProps=调度=>{
返回{
filterItemsByTagName:selectedTags=>
分派(actions.filterItemsByTagName(SelectedTag))
};
};
使用路由器导出默认值(connect(null,mapDispatchToProps)(NavBar))您需要集成

在redux进行浅层比较以检查组件是否需要更新时,更新状态似乎没有反映在容器中

import {Router} from 'react-router-dom';
import { ConnectedRouter, routerReducer, routerMiddleware } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';

const history = createHistory()
const middleware = routerMiddleware(history)


const rootReducer = combineReducers({
    router: routerReducer,
  //other reducers
});

const store = createStore(rootReducer,applyMiddleware(middleware));

<Provider store={store}>
<ConnectedRouter>
 //routes
</ConnectedRouter>
</Provider>

我想这与你使用
有关。它创建自己的历史实例,并侦听该实例上的更改。因此,另一个实例将更改url,但不会更新
。相反,您可以使用
ConnectedRouter
并将历史记录实例作为

import createHistory from 'history/createBrowserHistory';
...
const history = createHistory();
...
<ConnectedRouter history={history}>
...
</ConnectedRouter>
从“历史记录/createBrowserHistory”导入createHistory;
...
const history=createHistory();
...
...

createhistory从何处来(即从何处导入)?更新答案。您需要重新安装。谢谢!我在哪里加上这个,在减速机里?我有几个减缩器..您必须使用
combinereducer
组合您的减缩器,如
rootReducer
所示
import createHistory from 'history/createBrowserHistory';
...
const history = createHistory();
...
<ConnectedRouter history={history}>
...
</ConnectedRouter>