Eslint warning about super(props) removing it breaks code

Kid

I learn React and read code and now I see this Eslint warning

(props(

I refactor the code like this: removing props

constructor() {
    super();
    this.state = this.parse();
}

Now Eslint is happy but the code breaks because of probably props cant be used in the this.parse();

How is this suppose to work?

Completet code:

// Copyright (c) 2017 PlanGrid, Inc.

import React, { Component } from 'react';
import XLSX from 'xlsx';

import CsvViewer from './csv-viewer';
import WithFetching from '../file-viewer/fetch-wrapper';

class XlxsViewer extends Component {
    constructor(props) {
        super(props);
        this.state = this.parse();
    }

    parse() {
        const { data } = this.props;
        const dataArr = new Uint8Array(data);
        const arr = [];

        for (let i = 0; i !== dataArr.length; i += 1) {
            arr.push(String.fromCharCode(dataArr[i]));
        }

        const workbook = XLSX.read(arr.join(''), { type: 'binary' });
        const names = Object.keys(workbook.Sheets);
        const sheets = names.map(name => XLSX.utils.sheet_to_csv(workbook.Sheets[name]));

        return { sheets, names, curSheetIndex: 0 };
    }

    renderSheetNames(names) {
        const sheets = names.map((name, index) => (
            <input
                key={name}
                type="button"
                value={name}
                onClick={() => {
                    this.setState({ curSheetIndex: index });
                }}
            />
        ));

        return <div className="sheet-names">{sheets}</div>;
    }

    renderSheetData(sheet) {
        const csvProps = { ...this.props, data: sheet };
        return <CsvViewer {...csvProps} />;
    }

    render() {
        const { sheets, names, curSheetIndex } = this.state;
        return (
            <div className="spreadsheet-viewer">
                {this.renderSheetNames(names)}
                {this.renderSheetData(sheets[curSheetIndex || 0])}
            </div>
        );
    }
}

export default WithFetching(XlxsViewer);
Osama Sayed

An advice, move the parse() part to componentDidMount(). the constructor should be used in two cases only and shouldn't have any side-effects:

  • Initializing local state by assigning an object to this.state.
  • Binding event handler methods to an instance.

Also, not calling super(props) will result in you not being able to have the actual values inside this.props as it will be undefined.

A refactor to this code using this.setState() would look like:

import React, { Component } from "react";
import XLSX from "xlsx";

import CsvViewer from "./csv-viewer";
import WithFetching from "../file-viewer/fetch-wrapper";

class XlxsViewer extends Component {
  componentDidMount() {
    this.parse();
  }

  parse() {
    const { data } = this.props;
    const dataArr = new Uint8Array(data);
    const arr = [];

    for (let i = 0; i !== dataArr.length; i += 1) {
      arr.push(String.fromCharCode(dataArr[i]));
    }

    const workbook = XLSX.read(arr.join(""), { type: "binary" });
    const names = Object.keys(workbook.Sheets);
    const sheets = names.map((name) =>
      XLSX.utils.sheet_to_csv(workbook.Sheets[name])
    );

    this.setState({ sheets, names, curSheetIndex: 0 });
  }
  ...
}

export default WithFetching(XlxsViewer);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

removing printf statment from code breaks program

From Dev

Code Analysis Warning about a Constructor

From Java

Typescript | Warning about Missing Return Type of function, ESLint

From Dev

removing py files and retaining pyc files breaks inspection code

From Dev

Adding/removing some tabs and line breaks in an HTML code using sed

From Dev

Removing Glimpse breaks sitecore

From Dev

eslint warning interpretation

From Dev

Warning throwed breaks compilation

From Dev

Is Jetbrains dotPeek giving me a warning about my code?

From Dev

Is Jetbrains dotPeek giving me a warning about my code?

From Dev

Removing specific line breaks in Python

From Dev

About Removing a cookie in PHP

From Dev

About removing repos

From Dev

Removing "eliminated by erasure" warning in Scala

From Dev

Removing Unused parameters warning in QtCreator

From Dev

Removing Unused parameters warning in QtCreator

From Java

eslint complaining about resolving paths

From Dev

Removing html line breaks using Javascript

From Dev

android eclipse removing jar breaks private libraries

From Dev

stop jquery append removing line breaks

From Dev

NSLineBreakByTruncatingTail on UITextView removing new line breaks

From Dev

Removing line breaks from bibtex file in emacs

From Dev

removing unnecessary line breaks using regex

From Dev

Removing line breaks from log files

From Dev

Removing turbolinks breaks Actioncable chat room

From Dev

Preserve line breaks when Removing Bookmarked Lines

From Java

ESLint not working in VS Code?

From Dev

Eslint is unstable in vs code

From Dev

Omitting <?> unintuitively breaks this code