Giter Site home page Giter Site logo

Comments (10)

adidahiya avatar adidahiya commented on May 18, 2024 9

Doesn't that result in the same issues as any other lambda in a JSX prop? Personally I like using a pattern like this for ref handlers:

class Button extends React.Component<{}, {}> {
    private buttonElement: HTMLElement;
    private refHandlers = {
        button: (ref) => this.buttonElement = ref,
        ...
    };

    public render() {
        return <button ref={this.refHandlers.button} />;
    }
}

@jkillian any thoughts on a whitelist of exceptions for this rule?

from tslint-react.

jkillian avatar jkillian commented on May 18, 2024 4

A whitelist seems like unnecessary extra complexity to me. If a lamda is needed, it can be declared and initialized before the JSX code.

As far as the case of ref, there's no need to recreate the lambda on every render, which is exactly what this rule is trying to prevent. Something like the code example above (the object literal isn't necessary) works well.

from tslint-react.

mitchlloyd avatar mitchlloyd commented on May 18, 2024 1

@adidahiya wrote:

I would accept a PR to whitelist certain props like ref.

However #107, just permitted "ref" and did not introduce a whitelist.

Hard to say how significant the performance impact is that @shahan312 is raising (I'm generally onboard with @testerez's line of thinking), but it seems as reasonable a concern as any I've seen on this topic. My own interest in a whitelist is to allow anonymous functions for render props which are often used with closure scoping (e.g. <Route /> from react-router).

With this in mind, I've opened a PR to introduce a configurable whitelist. Please let me know what you think!

from tslint-react.

jkillian avatar jkillian commented on May 18, 2024

Closing, as I don't think we'll take any action here

from tslint-react.

testerez avatar testerez commented on May 18, 2024

@jkillian in react the performance drop with arrow function as prop is due to pure components re-rendering when it could be avoided. The creation on a new function is not a problem.

The ref attribute is not part of the props so it's not a problem to use an arrow function here.

See: facebook/react#9086 (comment)

All React examples I could find recommend the usage of an arrow function in ref and alternatives seem needlessly complicated.

IMO the fact that this rule warns on ref makes it pretty useless.

from tslint-react.

adidahiya avatar adidahiya commented on May 18, 2024

The ref attribute is not part of the props so it's not a problem to use an arrow function here.

This is a good point. I would accept a PR to whitelist certain props like ref.

from tslint-react.

bigshahan avatar bigshahan commented on May 18, 2024

ref is not part of the props, but would like the ability to keep the linter error for ref attribute, as when it is provided as a lambda, the ref gets called to unset it and then again to set it again, on every render. Should I create a new PR to bring in as an option?

Source: https://reactjs.org/docs/refs-and-the-dom.html#caveats

from tslint-react.

testerez avatar testerez commented on May 18, 2024

@shahan312 did you mesure any actual performance loss related to that?

from tslint-react.

msaspence avatar msaspence commented on May 18, 2024

@adidahiya how about the ability to allow on DOM components where he performance implications would be limited?

Essentially this suggestion for eslint jsx-eslint/eslint-plugin-react#1238

from tslint-react.

adidahiya avatar adidahiya commented on May 18, 2024

Closing due to deprecation, see #210

from tslint-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.