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

Minor cleanup for the React styleguide #619

Merged
merged 3 commits into from
Dec 23, 2015

Conversation

kesne
Copy link
Contributor

@kesne kesne commented Dec 14, 2015

This fixes some minor things that I thought were missing from the styleguide.

If people are unhappy about enforcing names for event handlers or additional render methods, I'm happy to discuss/remove.


  • Obey our own styleguide for modules.
  • Added missing rule for true props.
  • New rule on event handler binding.

@@ -69,13 +69,13 @@
**Component Naming**: Use the filename as the component name. For example, `ReservationCard.jsx` should have a reference name of `ReservationCard`. However, for root components of a directory, use `index.jsx` as the filename and use the directory name as the component name:
```javascript
// bad
const Footer = require('./Footer/Footer.jsx')
import Footer from './Footer/Footer.jsx';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we omit the extension in all our examples? file extensions should ideally never be specified in a require

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb 👍

@koistya
Copy link

koistya commented Dec 14, 2015

Prefix the name of event handlers with on.

<button onClick={handleClick}>Click me</button>
                     
                     └─ (verb) component's action
           └─ (noun) component's property

In many cases the event handlers (actions) should be named so that it's easy to understand what a particular function is doing. For example:

<button onClick={pokePlayer}>Poke Player</button>
                     
                     └─ (verb) a function performing a specific action
           └─ (noun) a property holding a reference to the component's action(s)

In both these examples using the on prefix for action names (as this PR suggests) doesn't make any sense to me. I would rather prefix my components' actions with verbs (handleClick, deleteUser, closeModal etc.).

@ljharb
Copy link
Collaborator

ljharb commented Dec 14, 2015

That is a pretty convincing counterpoint.

@kesne
Copy link
Contributor Author

kesne commented Dec 14, 2015

The one issue with that is it makes the sort-comp rule pretty useless, assuming those methods exist on the component.

@ljharb
Copy link
Collaborator

ljharb commented Dec 15, 2015

That is true. However, I'm not convinced we want to retain that rule (obv it'll need further discussion)

@kesne
Copy link
Contributor Author

kesne commented Dec 15, 2015

I'm happy to drop that from this PR and leave it for another conversation.

@ljharb
Copy link
Collaborator

ljharb commented Dec 15, 2015

@kesne yeah let's split the naming section into a separate PR, and keep the rule fix and the rest of the guide cleanups?

@charpeni
Copy link

I think we should use arrow functions instead of .bind(this), it's more ES6.

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

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

    onClickDiv() {
      // do stuff
    }

    render() {
      return <div onClick={this.onClickDiv} />
    }
  }

// good
  class extends React.Component {
    constructor(props) {
      super(props);
    }

    onClickDiv = () => {
      // do stuff
    }

    render() {
      return <div onClick={this.onClickDiv} />
    }
  }

@ljharb
Copy link
Collaborator

ljharb commented Dec 17, 2015

@charpeni That would require the class properties proposal, which we don't yet use at Airbnb because it's too far from being finalized.

@goatslacker
Copy link
Collaborator

it's more ES6.

It's actually not even ES7 😭

@charpeni
Copy link

@ljharb I did not noticed that class properties was only a proposal, thank you for the clarification.

@ljharb
Copy link
Collaborator

ljharb commented Dec 23, 2015

LGTM pending one final rebase. (This is a breaking change, fwiw)

@kesne kesne force-pushed the jgens/react-cleanup branch from 1f65662 to e12e5f0 Compare December 23, 2015 22:57
ljharb added a commit that referenced this pull request Dec 23, 2015
[eslint config] [breaking] [react] Minor cleanup for the React styleguide
@ljharb ljharb merged commit fcd1c1d into airbnb:master Dec 23, 2015
@kesne kesne deleted the jgens/react-cleanup branch December 23, 2015 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants