question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react/prefer-stateless-function with PureComponent.

See original GitHub issue

I’m using:

"react/prefer-stateless-function": [1, { "ignorePureComponents": true }]

but I’m also using this new Query component by react-apollo:

import React, { PureComponent } from "react";
import gql from "graphql-tag";
import { Query } from "react-apollo";

const GET_DOGS = gql`
  {
    dogs {
      id
      breed
    }
  }
`;

class DogsLove extends PureComponent {
    render() {
        return (
            <Query query={GET_DOGS}>
                {({ loading, error, data }) => {
                if (loading) return "Loading...";
                if (error) return `Error! ${error.message}`;

                return (
                    <select name="dog" onChange={onDogSelected}>
                    {data.dogs.map(dog => (
                        <option key={dog.id} value={dog.breed}>
                        {dog.breed}
                        </option>
                    ))}
                    </select>
                );
                }}
            </Query>
        )
    }
}

So, why this is a warning? (Or an error if I disable ignorePureComponents?)

I just need PureComponent because I don’t need to re-render DogsLove component every time I switch Route. I need it to render just one time, the first time. Then Query component is making an observable and nothing more.

Where am I wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
callumlockecommented, May 31, 2018

I have the same problem. ignorePureComponents: true doesn’t seem to do anything.

Doesn’t matter if I extend PureComponent or React.PureComponent.

7reactions
FlaviooLimacommented, Feb 18, 2019

This bug need some attention 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-react/prefer-stateless-function.md at master
Enforce stateless components to be written as a pure function ( react/prefer-stateless-function ) Stateless functional components are simpler than class based  ...
Read more >
Component should be written as a pure function (react prefer ...
Whenever you don't need state of lifecycle methods, you should write your component as a stateless function, as stateless components are in ...
Read more >
What are React pure functional components? - LogRocket Blog
A React component is considered pure if it renders the same output for the same state and props. For this type of class...
Read more >
eslint-plugin-react-prefer-function-component - npm
eslint-plugin-react/prefer-stateless-function allows class components that implement any methods or properties other than render .
Read more >
Component should be written as a pure function (react prefer ...
It will work for sure. Write your component as a stateless function: export myComponent = () => { //stuff here }; There are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found