【问题标题】:Unable to pass correct component as props to a custom router无法将正确的组件作为道具传递给自定义路由器
【发布时间】:2020-11-12 09:27:58
【问题描述】:

我正在尝试创建一个<ProtectedRoute /> 组件,我可以在其中传递一个如下所示的组件

<Route exact={true} path={routes.LOGIN} component={Login} />
<ProtectedRoute path={routes.HOME} component={Home} authUser={this.state.authUser} />
<ProtectedRoute path={routes.PROJECT} component={Project} authUser={this.state.authUser} />

我的 ProtectedRoute.tsx 代码

interface ProtectedRouteProps {
    path: string;
    component: React.ElementType;
    authUser: any;
}

export class ProtectedRoute extends React.Component<ProtectedRouteProps, {}> {

    constructor(props: any) {
        super(props);
    }

    render() {
        const Component = this.props.component;
        const authUser = this.props.authUser;
        console.log(this.props.component);

        return authUser ? (
            <Component />
        ) : (
                <Redirect to={{ pathname: routes.LOGIN }} />
            );
    }
}

在道具中我也试过React.ComponentReact.ComponentType&lt;any&gt;

但是,我的应用从不路由到 Project,它总是路由到 Home

所以,我添加了一个日志以查看我得到的this.props.component

我总是得到

class Home extends react__WEBPACK_IMPORTED_MODULE_0___default.a.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.c…

这让我觉得我没有正确传递组件。

任何人都可以帮助解决错误或如何使用道具传递组件。 PS:如果我在Route 中添加Project 而不是ProtectedRoute,它可以正常工作。

编辑:添加 Home/Project 组件(两者具有相同的形式)。

export class Project extends React.Component {
    constructor(props: any) {
        super(props);
    }

    render() {
        return (
            <div>
                <h1>Project</h1>
            </div>
        )
    }
}

【问题讨论】:

    标签: javascript reactjs typescript react-router


    【解决方案1】:

    你需要发送组件

    <Route exact={true} path={routes.LOGIN} component={Login} />
    <ProtectedRoute path={routes.HOME} component={()=>(<Home/>)} authUser={this.state.authUser} />
    <ProtectedRoute path={routes.PROJECT} component={()=>(<Project/>)} authUser={this.state.authUser} />
    

    和 ProtectedRoute

    interface ProtectedRouteProps {
    path: string;
    component: React.ElementType;
    authUser: any;
    }
    
    export class ProtectedRoute extends React.Component<ProtectedRouteProps, {}> {
    
    constructor(props: any) {
        super(props);
    }
    
    render() {
        const authUser = this.props.authUser;
        console.log(this.props.component);
    
        return authUser ? (
            this.props.component()
        ) : (
                <Redirect to={{ pathname: routes.LOGIN }} />
            );
    }
    

    }

    【讨论】:

    • 如果我按照您所写的操作,我会收到以下错误 -> This expression is not callable. Not all constituents of type 'ElementType&lt;any&gt;' are callable. Type '"symbol"' has no call signatures. TS2349 。我按照您的建议更改了App.tsx,并将返回值保留为&lt;Component /&gt;,这也不起作用。
    • 你能用这个问题创建一个最小的codebox吗,没有env很难调试
    • 否则你可以参考我所说的render prop或者react中的HOC
    猜你喜欢
    • 2019-06-08
    • 2017-05-29
    • 2021-09-11
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多