反应路由器程序重定向到具有多个参数的路由不起作用

问题描述

我正在尝试通过具有两个动态参数的url将用户重定向回先前的路由。但是,添加这两个参数会使重定向成为无操作。

当我在没有他们的情况下重定向时,它可以正常工作。也可以通过url访问路由。它实际上似乎是程序重定向。我尝试过推送到历史记录堆栈,并使用Link div-无济于事。谁能将我推向正确的方向?

url:入职/ 5f4a3ee7e28892ae792a0107 / 5f4a3ee7e28892ae792a0105


    goTo(redirectTo) {
        this.setState({ redirectTo });
    }

    renderRedirect() {
        const { redirectTo } = this.state
        const path = `/${redirectTo}`;

        if (!redirectTo) {
            return;
        }
 
        return <Redirect to={path}/>
    }

    render() {
        return (
            <BrowserRouter>
         {this.renderRedirect()}
                <div
                    className={`App ${
                        window.location.pathname !== '/admin' ? 'view' : ''
                    }`}
                >
                    <SchedulePanel />
                    <img className="voxpop-logo" src={VoxPopLogo} alt="logo" />
                    <Switch>
                        <Route
                            path="/onboarding/:eventId/:groupId"
                            exact
                            component={OnboardingView}
                        />
                        <Route path="/profile" exact component={ProfileView} />
                        <Route
                            path="/questions"
                            exact
                            component={QuestionsView}
                        />
                        <Route
                            path="/searching"
                            exact
                            component={SearchingView}
                        />
                        <Route path="/match" exact component={MatchView} />
                        <Route path="/chat" exact component={ChatView} />
                        <Route
                            path="/offboarding"
                            exact
                            component={OffboardingView}
                        />
                        <Route path="/admin" exact component={AdminView} />
                        <Route path="/no-match" exact component={NoMatch} />
                        <Route path="/dev" exact component={Dev}/>
                    </Switch>
                </div>
            </BrowserRouter>
        );
    }

解决方法

我注意到您在<Route />路线的/onboarding/:eventId/:groupId中,您拥有exact道具。这可能是问题所在,因为根据docsexact上的<Route />属性表示URL必须完全匹配 。当然,动态路径不会出现这种情况。

在没有exact道具的情况下尝试一下,看看它是否可以解决!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...