gpt4 book ai didi

reactjs - 在history.push()之后未呈现组件

转载 作者:行者123 更新时间:2023-12-03 17:42:35 27 4
gpt4 key购买 nike

在单击按钮时,我通过执行history.push()来更改URL

import createHistory from 'history/createBrowserHistory'  
const history = createHistory()
.
. some code
.
history.push(`/share/${objectId}`)

并希望该URL的 Route中提到的组件可以呈现,但是没有发生。但是,在刷新时,该组件正按预期方式呈现。但是我不明白为什么URL更改后它不立即呈现。我试过将组件包装在 withRouter中。
import React, {Component} from 'react'  
import {BrowserRouter, Router, Route, Switch, withRouter} from 'react-router-dom'
import createHistory from 'history/createBrowserHistory'

import Home from './pages/home'
import ViewFile from './pages/view-file'

const history = createHistory()

class App extends Component {
constructor(props) {
super(props)
}

render() {
return (
<BrowserRouter>
<Switch>
<Route exact path={'/'} component={Home}/>
<Route path={'/share/:id'} component={withRouter(ViewFile)}/>
</Switch>
</BrowserRouter>
)
}
}

export default App

以及通过 Router传递历史记录(我认为与使用 BrowserRouter相同)。
import React, {Component} from 'react'  
import {BrowserRouter, Router, Route, Switch, withRouter} from 'react-router-dom'
import createHistory from 'history/createBrowserHistory'

import Home from './pages/home'
import ViewFile from './pages/view-file'

const history = createHistory()

class App extends Component {
constructor(props) {
super(props)
}

render() {
return (
<Router history={history}>
<Switch>
<Route exact path={'/'} component={Home}/>
<Route path={'/share/:id'} component={ViewFile}/>
</Switch>
</Router>
)
}
}

export default App

但没有得到任何运气。谁能解释为什么会这样?
附言:我经历了答案 here,但他们没有帮助

最佳答案

每次调用createHistory()时,您都在创建新的历史记录。如果您使用的是react-router-dom,则只需使用withRouter HOC,它将通过historyprop对象提供给组件。然后,您将利用history.push('/'),或者如果它在class componentthis.props.history.push('/')中,等等。

工作示例:https://codesandbox.io/s/p694ln9j0

路由(在history中定义routes)

import React from "react";
import { Router, Route, Switch } from "react-router-dom";
import { createBrowserHistory } from "history";
import Header from "../components/Header";
import Home from "../components/Home";
import About from "../components/About";
import Contact from "../components/Contact";
import ScrollIntoView from "../components/ScrollIntoView";

const history = createBrowserHistory();

export default () => (
<Router history={history}>
<div>
<ScrollIntoView>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
<Header />
</ScrollIntoView>
</div>
</Router>
);

组件/Header.js (但是,我们想访问 history,但是有时我们不得不使用 withRouter,因为诸如 Header之类的组件并不位于 <Route "/example" component={Header}/> HOC内,因此它没有意识到我们的路由)
import React from "react";
import { withRouter } from "react-router-dom";

const Header = ({ history }) => (
<header>
<nav style={{ textAlign: "center" }}>
<ul style={{ listStyleType: "none" }}>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/")}>Home</button>
</li>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/about")}>About</button>
</li>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/contact")}>Contact</button>
</li>
</ul>
</nav>
</header>
);

export default withRouter(Header);

组件/Home.js (像 Home这样的组件知道路由,并且已经通过 history HOC传递了 <Route path="/" component={Home} />对象,因此不需要 withRouter)
import React from "react";

const Home = ({ history }) => (
<div className="container">
<button
className="uk-button uk-button-danger"
onClick={() => history.push("/notfound")}
>
Not Found
</button>
<p>
...
</p>
</div>
);

export default Home;

但是,如果您不想使用 withRouter,则与上面的概念相同,则只需创建一个 history实例即可在需要它的组件之间共享。您将对该 import实例进行 history并使用 history.push('/');进行导航,依此类推。

工作示例: https://codesandbox.io/s/5ymj657k1k

历史记录(在自己的文件中定义 history)
import { createBrowserHistory } from "history";

const history = createBrowserHistory();

export default history;

路由(将 history导入 routes)
import React from "react";
import { Router, Route, Switch } from "react-router-dom";
import Header from "../components/Header";
import Home from "../components/Home";
import About from "../components/About";
import Contact from "../components/Contact";
import ScrollIntoView from "../components/ScrollIntoView";
import history from "../history";

export default () => (
<Router history={history}>
<div>
<ScrollIntoView>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
<Header />
</ScrollIntoView>
</div>
</Router>
);

组件/Header.js (将 history导入 Header)
import React from "react";
import history from "../history";

const Header = () => (
<header>
<nav style={{ textAlign: "center" }}>
<ul style={{ listStyleType: "none" }}>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/")}>Home</button>
</li>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/about")}>About</button>
</li>
<li style={{ display: "inline", marginRight: 20 }}>
<button onClick={() => history.push("/contact")}>Contact</button>
</li>
</ul>
</nav>
</header>
);

export default Header;

components/Home.js (仍然知道路由,并且已经通过 history HOC传递了 <Route path="/" component={Home} />对象,因此不需要导入 history,但是如果愿意,您仍然可以导入它-只是不需要在 history的函数参数中解构 Home)
import React from "react";

const Home = ({ history }) => (
<div className="container">
<button
className="uk-button uk-button-danger"
onClick={() => history.push("/notfound")}
>
Not Found
</button>
<p>
...
</p>
</div>
);

export default Home;

但是您可能会想, BrowserRouter呢?好吧, BrowserRouter有它自己的内部 history对象。我们可以再次使用 withRouter来访问其 history。在这种情况下,我们甚至根本不需要 createHistory()!

工作示例: https://codesandbox.io/s/ko8pkzvx0o

路线
import React from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Header from "../components/Header";
import Home from "../components/Home";
import About from "../components/About";
import Contact from "../components/Contact";
import Notfound from "../components/Notfound";
import ScrollIntoView from "../components/ScrollIntoView";

export default () => (
<BrowserRouter>
<div>
<ScrollIntoView>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route component={Notfound} />
</Switch>
<Header />
</ScrollIntoView>
</div>
</BrowserRouter>
);

关于reactjs - 在history.push()之后未呈现组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55130393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com