在路径中传递参数 [英] Passing parameters in route

查看:82
本文介绍了在路径中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将参数传递到路由中?

Is there a way to pass parameters into a route ?

我有这个:

< PrivateRoute path ="/testpage"component = {TestPage}/>

还有

const PrivateRoute = ({ path, component }) => {
    const { isAuthenticated } = useContext(AuthContext);

    return isAuthenticated ? (
        <Route path={path} component={component} />
    ) : (
            <Redirect to="/login" />
        );
};

我想做这样的事情(在我的路线中添加myParameter):

And I want to do something like this (adding myParameter in my route):

<PrivateRoute path="/testpage" component={TestPage} myParameter={myParameter} />

谢谢

推荐答案

将组件名称更改为Component

Change name of component to Component

const PrivateRoute = ({ path, Component, myParameter }) => {
    const { isAuthenticated } = useContext(AuthContext);

    return isAuthenticated ? (
        <Route path={path} render={() => <Component myParameter={myParameter} />} />
    ) : (
            <Redirect to="/login" />
        );
};

如何呈现您的私路:

<PrivateRoute path="/testpage" Component={TestPage} myParameter={myParameter} />

这篇关于在路径中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆