简体   繁体   English

如何编写返回 class 组件作为功能组件的 React 组件?

[英]How can I write React component that returns a class component as a functional component?

My Goal:我的目标:

I'm currently implementing an AuthUserRole HOC component to handle the user roles: Manager and Employee.我目前正在实现一个AuthUserRole HOC 组件来处理用户角色:经理和员工。 I'm using this tutorial to do so, but they use a functional component to return a class component.我正在使用本教程这样做,但他们使用功能组件来返回 class 组件。

My Question:我的问题:

How can I write this HOC functional component to return a functional component instead of a class component?如何编写此 HOC 功能组件以返回功能组件而不是 class 组件? Or is there a better way?或者,还有更好的方法?

Functional Component That Returns A Class Component:返回 Class 组件的功能组件:

// Imports: Dependencies
import React, { Component } from 'react';
import { connect } from 'react-redux';

// Component: Authorization
const AuthUserRole = (WrappedComponent, allowedRoles) => {
  class With AuthUserRole extends Component {
    render() {
      const userType  = this.props.userType;
      if (allowedRoles.includes(userType)) {
        return <WrappedComponent {...this.props} />;
      }
      else {
        return <h1>You are not allowed to view this page!</h1>;
      }
    }
  }

  const mapStateToProps = state => ({ user: state.login.userName, userType: state.login.userType });

  return connect(mapStateToProps)(AuthUserRole);
};

// Exports
export default AuthUserRole;

App.js (Where HOC Is Used): App.js(使用 HOC 的地方):

<BrowserRouter history={history}>
  <Switch>
    {/* LANDING */}
    <Route path="/" component={Landing} exact />

    {/* APP */}
    <PrivateRoute path="/student/dashboard" component={Authorization(DashboardStudent,["Student"])}/>
    <PrivateRoute path="/admin/dashboard" component={Authorization(DashboardAdmin,["Admin"])}/>

    {/* LOGIN */}
    <Route path="/login" component={UserLogin}/>  

    {/* NOT FOUND */}
    <Route path="" component={NotFoundPage} />
  </Switch>
</BrowserRouter>

In order to return a function component, just return a function of props为了返回一个 function 组件,只需返回一个 function 的props


const AuthUserRole = (WrappedComponent, allowedRoles) => {
   return (props) => {
...
   }
}

The recommended best practice is to use the useSelector hook instead of mapStateToProps and connect . 推荐的最佳实践是使用useSelector挂钩而不是mapStateToPropsconnect So there are some other tweaks.所以还有一些其他的调整。

import React, { ComponentType } from 'react';
import { useSelector } from 'react-redux';

// Component: Authorization
const AuthUserRole = <Props extends {}>(WrappedComponent: ComponentType<Props>, allowedRoles: string[]) => {
  return (props: Props) => {
      // TODO: add your app's state type
      const userType = useSelector(state => state.login.userType);
      const user = useSelector(state => state.login.userName); // where is this used?

      if (allowedRoles.includes(userType)) {
        return <WrappedComponent {...props} />;
      }
      else {
        return <h1>You are not allowed to view this page!</h1>;
      }

  }
};

// Exports
export default AuthUserRole;

I added type annotations because you tagged this typescript , but I don't see any types in the current code.我添加了类型注释,因为您标记了这个typescript ,但我在当前代码中看不到任何类型。

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

相关问题 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 如何编写扩展功能组件的ES6类React组件? - How to write an ES6 class React Component that extends a functional component? 如何使用反应功能组件添加或删除 class - How can I add or remove class using react functional component 如何反应才能识别它是 class 组件还是功能组件? - How react can Identify it's a class component or functional component? 将类组件反应为功能组件 - React class component to functional component 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? 将 React 类组件转换为具有许多返回值的函数式组件 - Convert React Class Component to Functional Component with many returns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM