Skip to content

prefer-stateless with event handlers is problematic #784

Closed
@kesne

Description

@kesne

I noticed with the latest version of the config that I get a lot of prefer-stateless warnings on components that just use props and event handlers. However, this is hard to do when the react/jsx-no-bind rule is enabled, which stops you from making functions for event handlers in the render path.

For example, here's a component that only uses props and an event handler that the linter will tell me to turn into a stateless component.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);

    this.handleClicked = this.handleClicked.bind(this);
  }

  handleClicked() {
    this.props.onClicked(this.props.name);
  }

  render() {
    return (
      <button onClick={this.handleClicked}>Click Me</button>
    );
  }
}

However, in the attempt to do so, you need to create a function in the render path, which causes the react/jsx-no-bind lint rule to blow up on you.

function MyComponent({ onClicked, name }) {
  return (
    <div onClick={() => onClicked(name)}>Blow Me Up</div>
  );
}

You can work around this by defining the function outside of the JSX, but at that point you're just working around the linter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions