简体   繁体   English

React HOC返回一个匿名_class

[英]React HOC returns an anonymous _class

I'm using MaterialUI, more specifically the TextField component which I want to decorate somehow to handle validation state at a component level. 我正在使用MaterialUI,更具体地说是TextField组件,我想以某种方式修饰它以处理组件级别的验证状态。

I know about the HOC in react, which is a pattern that seems to be perfect for this. 我知道反应中的HOC,这种模式似乎是完美的。 But I have to return an anonymous class and therefore I cannot get the value of the TextField component as I should, because the type returned is _class instead of TextField . 但是我必须返回一个匿名类,因此我无法获得TextField组件的值,因为返回的类型是_class而不是TextField

Am I doing something wrong with the HOC pattern, or perhaps this is not the best way to reuse a component without modifying its prototype. 我是否对HOC模式做错了,或者这可能不是在不修改原型的情况下重用组件的最佳方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The HOC declaration HOC声明

export default function hocInputValidator( WrappedComponent ){
  return class extends Component{
    constructor(props){
      super(props);
    }

    handleChange(){
      console.log('Handling from HOC');
    }

    render(){
      return <WrappedComponent {...this.props}/>
    }
  }
}

The invocation and exporting of the decorated component 调用和导出装饰组件

export default hocInputValidator(TextField);

When I try to access the decorated component via refs , the type is _class instead of TextField . 当我尝试通过refs访问装饰组件时,类型是_class而不是TextField

HOCs are used mainly for cross cutting of concerns for an example Login. HOC主要用于交叉示例Login的示例。 Many components in your app may require Login functionality, hence you can wrap them with an HOC dynamically. 您的应用中的许多组件可能需要登录功能,因此您可以动态地用HOC包装它们。 An HOC will merely wrap whatever the component that is passed into it. HOC将仅包装传递给它的组件。 In your case I figured out few issues in your code. 在您的情况下,我在您的代码中发现了一些问题。 I have fixed them below. 我在下面修了它们。

export default function ( WrappedComponent ){
     class HocInputValidator extends Component{
        constructor(props){
          super(props);
        }

        handleChange(){
          console.log('Handling from HOC');
        }

        render(){
          return <WrappedComponent {...this.props}/>
        }
      }
      return HocInputValidator;

}

The invocation should be something like this. 调用应该是这样的。

import inputValidator from './components/input_validator';

inputvalidator(TextField);

Your HOC returns an anonymous class which is why it's being displayed as _class . 您的HOC返回一个匿名类,这就是它显示为_class

To work around this you can set the HOC's displayName to something descriptive which will override the _class display behaviour. 要解决此问题,您可以将HOC的displayName设置为描述性内容 ,以覆盖_class显示行为。

For instance, you can display the HOC as something like HigherOrderComponent(WrappedComponent) in the DOM which is more descriptive (and easier to debug). 例如,您可以将HOC显示为DOM中的HigherOrderComponent(WrappedComponent) ,它更具描述性(并且更易于调试)。 It's a convention followed by other libraries such as react-redux . 这是一个惯例,其次是其他库,如react-redux

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

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