Browse Source

docs: class properties and object spread are no longer experimental (#4754)

Co-authored-by: Romain Bohdanowicz <romain.bohdanowicz@formation.tech>
main
Romain Bohdanowicz 3 years ago
committed by GitHub
parent
commit
fb65da64ac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      content/docs/faq-functions.md
  2. 5
      content/docs/handling-events.md
  3. 2
      content/docs/hooks-intro.md
  4. 10
      content/docs/react-without-es6.md
  5. 2
      content/docs/typechecking-with-proptypes.md
  6. 2
      content/tutorial/tutorial.md

5
content/docs/faq-functions.md

@ -37,14 +37,13 @@ class Foo extends Component {
} }
``` ```
#### Class Properties (Stage 3 Proposal) {#class-properties-stage-3-proposal} #### Class Properties (ES2022) {#class-properties-es2022}
```jsx ```jsx
class Foo extends Component { class Foo extends Component {
// Note: this syntax is experimental and not standardized yet.
handleClick = () => { handleClick = () => {
console.log('Click happened'); console.log('Click happened');
} };
render() { render() {
return <button onClick={this.handleClick}>Click Me</button>; return <button onClick={this.handleClick}>Click Me</button>;
} }

5
content/docs/handling-events.md

@ -92,15 +92,14 @@ You have to be careful about the meaning of `this` in JSX callbacks. In JavaScri
This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method. This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method.
If calling `bind` annoys you, there are two ways you can get around this. If you are using the experimental [public class fields syntax](https://babeljs.io/docs/plugins/transform-class-properties/), you can use class fields to correctly bind callbacks: If calling `bind` annoys you, there are two ways you can get around this. You can use [public class fields syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#public_instance_fields) to correctly bind callbacks:
```js{2-6} ```js{2-6}
class LoggingButton extends React.Component { class LoggingButton extends React.Component {
// This syntax ensures `this` is bound within handleClick. // This syntax ensures `this` is bound within handleClick.
// Warning: this is *experimental* syntax.
handleClick = () => { handleClick = () => {
console.log('this is:', this); console.log('this is:', this);
} };
render() { render() {
return ( return (

2
content/docs/hooks-intro.md

@ -80,7 +80,7 @@ We'll discuss this more in [Using the Effect Hook](/docs/hooks-effect.html#tip-u
### Classes confuse both people and machines {#classes-confuse-both-people-and-machines} ### Classes confuse both people and machines {#classes-confuse-both-people-and-machines}
In addition to making code reuse and code organization more difficult, we've found that classes can be a large barrier to learning React. You have to understand how `this` works in JavaScript, which is very different from how it works in most languages. You have to remember to bind the event handlers. Without unstable [syntax proposals](https://babeljs.io/docs/en/babel-plugin-transform-class-properties/), the code is very verbose. People can understand props, state, and top-down data flow perfectly well but still struggle with classes. The distinction between function and class components in React and when to use each one leads to disagreements even between experienced React developers. In addition to making code reuse and code organization more difficult, we've found that classes can be a large barrier to learning React. You have to understand how `this` works in JavaScript, which is very different from how it works in most languages. You have to remember to bind the event handlers. Without [ES2022 public class fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#public_instance_fields), the code is very verbose. People can understand props, state, and top-down data flow perfectly well but still struggle with classes. The distinction between function and class components in React and when to use each one leads to disagreements even between experienced React developers.
Additionally, React has been out for about five years, and we want to make sure it stays relevant in the next five years. As [Svelte](https://svelte.dev/), [Angular](https://angular.io/), [Glimmer](https://glimmerjs.com/), and others show, [ahead-of-time compilation](https://en.wikipedia.org/wiki/Ahead-of-time_compilation) of components has a lot of future potential. Especially if it's not limited to templates. Recently, we've been experimenting with [component folding](https://github.com/facebook/react/issues/7323) using [Prepack](https://prepack.io/), and we've seen promising early results. However, we found that class components can encourage unintentional patterns that make these optimizations fall back to a slower path. Classes present issues for today's tools, too. For example, classes don't minify very well, and they make hot reloading flaky and unreliable. We want to present an API that makes it more likely for code to stay on the optimizable path. Additionally, React has been out for about five years, and we want to make sure it stays relevant in the next five years. As [Svelte](https://svelte.dev/), [Angular](https://angular.io/), [Glimmer](https://glimmerjs.com/), and others show, [ahead-of-time compilation](https://en.wikipedia.org/wiki/Ahead-of-time_compilation) of components has a lot of future potential. Especially if it's not limited to templates. Recently, we've been experimenting with [component folding](https://github.com/facebook/react/issues/7323) using [Prepack](https://prepack.io/), and we've seen promising early results. However, we found that class components can encourage unintentional patterns that make these optimizations fall back to a slower path. Classes present issues for today's tools, too. For example, classes don't minify very well, and they make hot reloading flaky and unreliable. We want to present an API that makes it more likely for code to stay on the optimizable path.

10
content/docs/react-without-es6.md

@ -134,7 +134,7 @@ var SayHello = createReactClass({
This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications. This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications.
If the boilerplate code is too unattractive to you, you may enable the **experimental** [Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal with Babel: If the boilerplate code is too unattractive to you, you may use [ES2022 Class Properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#public_instance_fields) syntax:
```javascript ```javascript
@ -143,11 +143,11 @@ class SayHello extends React.Component {
super(props); super(props);
this.state = {message: 'Hello!'}; this.state = {message: 'Hello!'};
} }
// WARNING: this syntax is experimental!
// Using an arrow here binds the method: // Using an arrow here binds the method:
handleClick = () => { handleClick = () => {
alert(this.state.message); alert(this.state.message);
} };
render() { render() {
return ( return (
@ -159,9 +159,7 @@ class SayHello extends React.Component {
} }
``` ```
Please note that the syntax above is **experimental** and the syntax may change, or the proposal might not make it into the language. You also have a few other options:
If you'd rather play it safe, you have a few options:
* Bind methods in the constructor. * Bind methods in the constructor.
* Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`. * Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.

2
content/docs/typechecking-with-proptypes.md

@ -177,7 +177,7 @@ const root = ReactDOM.createRoot(document.getElementById('example'));
root.render(<Greeting />); root.render(<Greeting />);
``` ```
If you are using a Babel transform like [plugin-proposal-class-properties](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties/) (previously _plugin-transform-class-properties_), you can also declare `defaultProps` as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser. For more information, see the [class fields proposal](https://github.com/tc39/proposal-class-fields). Since ES2022 you can also declare `defaultProps` as static property within a React component class. For more information, see the [class public static fields](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields#public_static_fields). This modern syntax will require a compilation step to work within older browsers.
```javascript ```javascript
class Greeting extends React.Component { class Greeting extends React.Component {

2
content/tutorial/tutorial.md

@ -542,7 +542,7 @@ var player = {score: 1, name: 'Jeff'};
var newPlayer = Object.assign({}, player, {score: 2}); var newPlayer = Object.assign({}, player, {score: 2});
// Now player is unchanged, but newPlayer is {score: 2, name: 'Jeff'} // Now player is unchanged, but newPlayer is {score: 2, name: 'Jeff'}
// Or if you are using object spread syntax proposal, you can write: // Or if you are using object spread syntax, you can write:
// var newPlayer = {...player, score: 2}; // var newPlayer = {...player, score: 2};
``` ```

Loading…
Cancel
Save