首页 > 解决方案 > React Router 更改了 URL 但页面仍然没有改变

问题描述

我使用自定义浏览器历史记录进行编程导航。为此,我以这种方式创造了历史:

import { createBrowserHistory } from 'history';

export default createBrowserHistory();

然后我把历史作为道具给了我的路由器,


const App=()=>{
    const error=()=>{
        return <h1>webpage was not found</h1>
    }
    return (
    <Router history={history}>
        <div className="ui container">
                <Header/>
                <Route path="/" exact component={StreamList}/>
                <Route path="/delete/:id" component={StreamDelete}/>
                <Route path="/edit/:id" component={StreamEdit}/>
                <Route path="/show/:id" component={StreamShow}/>
                <Route path="/new" component={StreamCreate}/>
        </div>
    </Router>
    )
}

操作完成后,我使用以下代码将我的应用程序从 StreamCreate 页面强制导航到 StreamList 页面。URL 改变,但页面没有改变。

export const createStream=(values)=>async(dispatch,getState)=>{
    const {userId}=getState().auth;
    const response=await jsonServer.post("/streams",{...values,userId});
    dispatch({type:'CREATE_POST', payload:response.data})
    history.push('/')
}

标签: reactjsreact-router

解决方案


推荐阅读