Search code examples
javascriptreactjseslinteslint-config-airbnb

Why am i getting ESLint - Component should be written as a pure function?


I am getting this eslint error, only for the app component "Component should be written as a pure function" and i am not sure why.

I checked other posts with this error and none of the solution seem to work.

import React from "react";
import { Switch, Route } from "react-router-dom";
import NotFound from "../Pages/Standalone/NotFoundDedicated";
import Auth from "./Auth";
import Application from "./Application";

import LoginDedicated from "../Pages/Standalone/LoginDedicated";
import ArticleNews from "./ArticleNews";
import ThemeWrapper, { AppContext } from "./ThemeWrapper";
window.__MUI_USE_NEXT_TYPOGRAPHY_VARIANTS__ = true;

class App extends React.Component {
  render() {
    return (
      <ThemeWrapper>
        <AppContext.Consumer>
          {changeMode => (
            <Switch>
              <Route path="/" exact component={LoginDedicated} />
              <Route
                path="/app"
                render={props => <Application {...props} changeMode={changeMode} />}
              />
              <Route
                path="/blog"
                render={props => <ArticleNews {...props} changeMode={changeMode} />}
              />
              <Route component={Auth} />
              <Route component={NotFound} />
            </Switch>
          )}
        </AppContext.Consumer>
      </ThemeWrapper>
    );
  }
}

export default App;

Solution

  • Your component contains no state, but you are using the form that supports state. There is some overhead that comes with extending React.Component that can be avoided in this case.

    Just alter your class to look like

    function App(props) {
        return (
            <ThemeWrapper>
            <AppContext.Consumer>
              {changeMode => (
                <Switch>
                  <Route path="/" exact component={LoginDedicated} />
                  <Route
                    path="/app"
                    render={props => <Application {...props} changeMode={changeMode} />}
                  />
                  <Route
                    path="/blog"
                    render={props => <ArticleNews {...props} changeMode={changeMode} />}
                  />
                  <Route component={Auth} />
                  <Route component={NotFound} />
                </Switch>
              )}
            </AppContext.Consumer>
          </ThemeWrapper>
        );
    }