1

I'm running into this problem that I'm not sure how to fix.

I'm coding React with Typescript and we have implemented Airbnb config for Typescript in our project. This is an example of my code:

class Grid extends React.PureComponent<GridProps, GridState> {
    private ref: React.RefObject<HTMLDivElement> = React.createRef();

    private location: History<any>;

    private history: Location<any>;


    public constructor(props: GridProps) {
        super(props);

        this.state = {
            rowHeight: 300,
            expandedCardId: this.props.expandedCardId || null,
            cardIdToScroll: null,
        };
    }

    private isCardsInfoReady = (): boolean => this.props.cardIds.length !== 0 && this.props.cardLayouts.length !== 0;

My problem is linter is yelling at the line of private Location that location should be placed after isCardsInfoReadyeslint(react/sort-comp);

This can be easily fixed by moving it or doing the following:

private location: History<any>|null = null;

As you can see, the moment I assign a value to it, linter is fine with me put it on the top. If I only assign in constructor, it still doesn't allow.

Since I don't like either way, I'm wondering whether there is a better work around on this.

Thanks!

Tree Nguyen
  • 1,198
  • 1
  • 14
  • 37
  • If `location` can be null, could you do something like `private location?: History` ? Also, I would recommend using hooks rather than classes (: – Baruch May 21 '19 at 06:20
  • @Baruch our team has a conversation about it and us afraid that the technology is still quite new and not stable yet. Therefore, we are not doing hooks atm. Regarding the first question, no, location should never be null. – Tree Nguyen May 21 '19 at 06:22
  • Hm, I will have to disagree with you and your team. Hooks has been tested in multiple internal apps in Facebook and there are many companies (AirBnB, Netflix) adapting their code to use them. They are extremely stable and that's the reason why they were released. In any case, maybe changing the method to a function statement instead of a property declaration? It's weird cause I use TS + AirBnB as well and I've never seen this linting error. – Baruch May 21 '19 at 06:25
  • I know that feeling and we are eager to use hooks to but that's not our decision to make. Compared to the age of the original class React components, Hooks is still so new. The arrow is from eslint(react/sort-comp) so maybe you guys have disabed it somehow? – Tree Nguyen May 21 '19 at 06:44

0 Answers0