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.

Allow exceptions to "jsx-no-lambda" rule

See original GitHub issue

I think the usage of lambdas in ref attributes can warrant an exception from the “jsx-no-lambda” rules, since this is an easy way to get type-safe access to rendered elements.

The only alternative I see would be to create instance functions which would be cumbersome to write and still have to be bound using .bind(this).

So until we have a nicer way to generate these “bindings” for example using macros, it would be nice to allow lambdas on some attributes.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
adidahiyacommented, Jul 25, 2016

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?

4reactions
jkilliancommented, Jul 25, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non lambda in React-Router V4 render function - Stack Overflow
@melbil This is not a typescript issue You can pass lambdas, the language allows it. You just have a rule in tslint (which...
Read more >
Typescript and React: Lambdas are forbidden? - Jon Hilton
One of those rules is called jsx-no-lambda. Here's the explanation (taken from the GitHub Repo). Creating new anonymous functions (with either ...
Read more >
Lambda expressions (since C++11) - cppreference.com
Dynamic exception specifications (until C++20) ... A lambda expression can use a variable without capturing it if the variable.
Read more >
Lambda event filtering - AWS Documentation
Lambda throws an exception at the time of the event source mapping creation or update. The filter pattern for data properties must be...
Read more >
Rules - ESLint - Pluggable JavaScript Linter
Rules in ESLint are grouped by type to help you understand their purpose. ... of optional chaining in contexts where the `undefined` value...
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