Typescript with React - use HOC on a generic component class

后端 未结 5 2030
别那么骄傲
别那么骄傲 2020-12-30 05:14

I have a generic React component, say like this one:

class Foo extends React.Component, FooState> {
    constructor(props: F         


        
5条回答
  •  梦谈多话
    2020-12-30 05:22

    Thanks for asking this question. I just figured out a way to specify a type parameter to a component after wrapping it with an HOC and I thought I'd share.

    import React from 'react';
    import withStyles from '@material-ui/core/styles/withStyles';
    import { RemoveProps } from '../helpers/typings';
    
    const styles = {
      // blah
    };
    
    interface Props {
      classes: any;
      items: T[];
      getDisplayName: (t: T) => string;
      getKey: (t: T) => string;
      renderItem: (t: T) => React.ReactNode;
    }
    
    class GenericComponent extends React.Component, State> {
      render() {
        const { classes, items, getKey, getDisplayName, renderItem } = this.props;
    
        return (
          
    {items.map(item => (
    {getDisplayName(item)}
    {renderItem(item)}
    ))}
    ); } } //

提交回复
热议问题