gpt4 book ai didi

javascript - 使用 React 自动重定向?

转载 作者:行者123 更新时间:2023-11-30 14:50:01 26 4
gpt4 key购买 nike

给定以下组件代码,有没有办法在给定的毫秒数后自动重定向到另一条路由?

import React, { Component } from "react";
import {
Route,
NavLink,
HashRouter
} from "react-router-dom";
import {
Container,
Button,
Col,
Row } from 'reactstrap'

class Confirm extends Component {
render() {
return (
<div>
<Container>
<Row>
<p>Reprehenderit reprehenderit laborum excepteur voluptate elit incididunt eu. Ex laboris ex officia do aute non. Dolor proident reprehenderit labore nostrud irure consectetur. Laboris laborum amet veniam tempor nostrud ad. Laborum culpa duis voluptate amet ipsum magna minim reprehenderit. Aute ipsum eiusmod amet occaecat culpa qui nostrud eiusmod dolore anim culpa amet nostrud. Id laborum tempor dolor labore veniam aliqua ipsum esse pariatur. Nisi ipsum eiusmod do eiusmod minim adipisicing aliqua. Nostrud tempor aliquip cupidatat sunt aute esse duis cupidatat culpa elit consectetur eu.</p>
</Row>
</Container>
</div>
);
}
}

export default Confirm;

查看文档,似乎在 react-router-dom 中似乎有一些功能,不是吗?

非常感谢。

编辑:在根据答案研究操纵一些东西之后,我做了一些改变并想出了这个:

import React, { Component } from "react";
import {
Route,
NavLink,
HashRouter,
BrowserRouter
} from "react-router-dom";
import {
Container,
Button,
Col,
Row } from 'reactstrap'
import './Confirm.css';

class Confirm extends Component {
componentDidMount() {
setTimeout(() => {
BrowserRouter.push("./home")
}, 2000)
}
render() {
return (
<div>
<Container>
<Row>
<p>Reprehenderit reprehenderit laborum excepteur voluptate elit incididunt eu. Ex laboris ex officia do aute non. Dolor proident reprehenderit labore nostrud irure consectetur. Laboris laborum amet veniam tempor nostrud ad. Laborum culpa duis voluptate amet ipsum magna minim reprehenderit. Aute ipsum eiusmod amet occaecat culpa qui nostrud eiusmod dolore anim culpa amet nostrud. Id laborum tempor dolor labore veniam aliqua ipsum esse pariatur. Nisi ipsum eiusmod do eiusmod minim adipisicing aliqua. Nostrud tempor aliquip cupidatat sunt aute esse duis cupidatat culpa elit consectetur eu.</p>
</Row>
</Container>
</div>
);
}
}

export default Confirm;

但由于某种原因推送不起作用。这是因为 hashrouterbrowserrouter 之间有冲突吗?

我得到的是这样的:

enter image description here

最佳答案

class Confirm extends Component {
componentDidMount() {
setTimeout(() => {
browserhistory.push("path/to/your/next/route")
}, 2000)
}
render() {
return (
<div>
<Container>
<Row>
<p>Reprehenderit reprehenderit laborum excepteur voluptate elit incididunt eu. Ex laboris ex officia do aute non. Dolor proident reprehenderit labore nostrud irure consectetur. Laboris laborum amet veniam tempor nostrud ad. Laborum culpa duis voluptate amet ipsum magna minim reprehenderit. Aute ipsum eiusmod amet occaecat culpa qui nostrud eiusmod dolore anim culpa amet nostrud. Id laborum tempor dolor labore veniam aliqua ipsum esse pariatur. Nisi ipsum eiusmod do eiusmod minim adipisicing aliqua. Nostrud tempor aliquip cupidatat sunt aute esse duis cupidatat culpa elit consectetur eu.</p>
</Row>
</Container>
</div>
);
}
}

这应该可以完成工作,使用 componentDidMount 因为它只会在生命周期中发生一次并且组件已安装

关于javascript - 使用 React 自动重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48294901/

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