简体   繁体   English

支持route hoc中react路由器的渲染道具

[英]support render props of react router in route hoc

I am writing a HOC for public route and private route. 我正在为公共路线和私人路线编写HOC。 If the route is private and the user is authenticated then let him/her enter that page else redirect to login component. 如果路由是私有的并且用户已通过身份验证,则让他/她输入该页面,否则将重定向到登录组件。 If the route is public and the user is not authenticated then show the page and also show the login page if the user is not authenticated but user is authenticated and still goes to login page then redirect the user to root page. 如果路由是公用的,并且用户未通过身份验证,则显示该页面,如果用户未通过身份验证但用户已通过身份验证并且仍然转到登录页面,则显示登录页面,然后将用户重定向到根页面。 This is working fine. 一切正常。 But if i use the render instead of component, then it does not work. 但是,如果我使用渲染器而不是组件,则它不起作用。 I could make it work only if i pass the component from the props called component of react-router. 仅当我传递来自称为react-router组件的props中的组件时,我才能使其工作。

How can i make it work if user user render props? 如果用户用户呈现道具,我该如何使其工作?

Here is my code 这是我的代码

<Switch>
  <PrivateRoute
    exact
    path="/"
    render={() => <Home name="something" />} {/* this does not work */}
  />
  <PrivateRoute exact path="/demo" component={Demo} />
  <PublicRoute restricted={true} path="/auth" component={Authentication} />
</Switch>

PublicRoute.js PublicRoute.js

const PublicRoute = ({component: Component, restricted, ...rest}) => {
  return (
    <Route
      {...rest}
      render={props =>
        isLogin() && restricted ? <Redirect to="/" /> : <Component {...props} />
      }
    />
  )
}

PrivateRoute.js PrivateRoute.js

const PrivateRoute = ({component: Component, ...rest}) => {
  return (
    <Route
      {...rest}
      render={props =>
        isLogin() ? <Component {...props} /> : <Redirect to="/auth/login" />
      }
    />
  )
}

Also if there is any additional things to improve, please do suggest me. 另外,如果还有其他需要改进的地方,请提出建议。

The problem is that in your custom routes you are always using the component prop. 问题在于,在您的自定义路线中,您始终使用component prop。 So when passing the render prop it is overruled by the one in your custom route and thus trying to render the provided component . 因此,当通过render道具时,它会被您的自定义路线中的一个道具否决,从而尝试渲染提供的component

When you modify it like the function below, it will work. 当您像下面的功能一样对其进行修改时,它将起作用。 It also extracts the render prop and if it's a function it will use that instead of the component prop. 它还提取了render道具,如果它是一个函数,它将使用它而不是component道具。

const PrivateRoute = ({component: Component, render, ...rest}) => {
    const renderContent = props => {
        if (!fakeAuth.isAuthenticated) {
            return (
                <Redirect
                    to={{
                        pathname: "/login",
                        state: { from: props.location }
                    }}
                />
            )
        }
        return (typeof render === 'function') ? render(props) : <Component {...props} />
    }

    return (
        <Route {...rest} render={renderContent} />
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM