Skip to content

Commit 4b5d55f

Browse files
committed
Update dated JavaScript syntax guidance and examples
1 parent 7690f86 commit 4b5d55f

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,36 @@ Ignore this if you're specifically describing `createElement`.
3838

3939
Ignore this if you're specifically writing about ES5.
4040

41-
#### Don't use ES6 features when equivalent ES5 features have no downsides.
41+
#### Prefer readable syntax.
4242

43-
Remember that ES6 is still new to a lot of people. While we use it in many places (`const` / `let`, classes, arrow functions), if the equivalent ES5 code is just as straightforward and readable, consider using it.
44-
45-
In particular, you should prefer named `function` declarations over `const myFunction = () => ...` arrows for top-level functions. However, you *should* use arrow functions where they provide a tangible improvement (such as preserving `this` context inside a component). Consider both sides of the tradeoff when deciding whether to use a new feature.
43+
Prefer named `function` declarations for components and top-level functions. Use arrow functions for inline callbacks when they make the example easier to follow.
4644

4745
#### Don't use features that aren't standardized yet.
4846

4947
For example, **don't** write this:
5048

5149
```js
52-
class MyComponent extends React.Component {
53-
state = {value: ''};
54-
handleChange = (e) => {
55-
this.setState({value: e.target.value});
50+
function Greeting({isLoggedIn}) {
51+
return do {
52+
if (isLoggedIn) {
53+
<h1>Welcome back!</h1>;
54+
} else {
55+
<h1>Please sign in.</h1>;
56+
}
5657
};
5758
}
5859
```
5960

61+
The [`do` expression syntax](https://github.com/tc39/proposal-do-expressions) is still experimental.
62+
6063
Instead, **do** write this:
6164

6265
```js
63-
class MyComponent extends React.Component {
64-
constructor(props) {
65-
super(props);
66-
this.handleChange = this.handleChange.bind(this);
67-
this.state = {value: ''};
68-
}
69-
handleChange(e) {
70-
this.setState({value: e.target.value});
66+
function Greeting({isLoggedIn}) {
67+
if (isLoggedIn) {
68+
return <h1>Welcome back!</h1>;
7169
}
70+
return <h1>Please sign in.</h1>;
7271
}
7372
```
7473

0 commit comments

Comments
 (0)