Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react/jsx-no-bind false positive? #1417

Closed
bcutler-work opened this issue Sep 7, 2017 · 6 comments · Fixed by #1459
Closed

react/jsx-no-bind false positive? #1417

bcutler-work opened this issue Sep 7, 2017 · 6 comments · Fixed by #1459

Comments

@bcutler-work
Copy link

Example snippet:

import React from 'react';
import PropTypes from 'prop-types';

class FalsePositive extends React.PureComponent {
  static propTypes = {
    list: PropTypes.arrayOf(PropTypes.node).isRequired,
  };

  render() {
    return (
      <div>
        {this.props.list.map(this.wrap.bind(this, 'span'))}
      </div>
    );
  }

  wrap(Tag, item, i) {
    return <Tag key={i}>{item}</Tag>;
  }
}

export default FalsePositive;

this.wrap.bind triggers the rule, even though the bound function isn't the actual prop being passed, but is being used during the render to build the children, so it shouldn't break purity.

@ljharb
Copy link
Member

ljharb commented Sep 8, 2017

Indeed, that should not complain because it's not passing the new function as a prop anywhere.

@jackyho112
Copy link
Contributor

I will give this one a look. 😄

@jackyho112
Copy link
Contributor

jackyho112 commented Sep 18, 2017

@ljharb

Seems like the rule needs a bit more fixing than I thought it would to only check for bind calls in JSX props. If that was its purpose, this test and this one should not warn but they are currently invalid tests.

The current logic in the rule checks for all bind calls in all JSX methods and warn each of them. And along with the documentation in that file, I am given the impression that this rule does check for bind calls in a JSX method instead of only those in JSX attributes.

I understand that based on the rule doc, it should only check for JSX props. However, based on the purpose of such checks, wouldn't it make sense to check for all bind calls in a method that returns JSX? Because all of those functions will be created again on every render like the case mentioned by the issue opener and that is the undesirable outcome the rule is trying to prevent.

This is just my 2 cents. If we have to write the rule to fulfill exactly its purpose as documented, I will probably have to refactor a portion of its logic, have some new logic to deal with aliases like what no-unused-state is doing, get rid of wrong tests and add new tests.

If my opinion seems reasonable, we would only have to improve the docs.

Let me know your thoughts. 😄

@ljharb
Copy link
Member

ljharb commented Sep 19, 2017

The rule is to avoid new functions being created and passed as props to an element (it should really apply not just to jsx, but to createElement calls as well).

@ljharb
Copy link
Member

ljharb commented Sep 19, 2017

It's hard to evaluate without an actual PR; I'd love to look at the test case and docs changes in particular that you're suggesting.

@jackyho112
Copy link
Contributor

jackyho112 commented Sep 19, 2017

@ljharb

Thanks for replying! I think I know the direction I'm going with to fix this issue now. Will open a PR soon. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants