diff --git a/guide/english/react/state/index.md b/guide/english/react/state/index.md index f50d3ce8db..6951e95620 100644 --- a/guide/english/react/state/index.md +++ b/guide/english/react/state/index.md @@ -4,23 +4,20 @@ title: State # State -State is the place where the data comes from. +State in React is an object with observable properties that determines how a component behaves and renders. -We should always try to make our state as simple as possible and minimize the number of stateful components. If we have, for example, ten components that need data from the state, we should create one container component that will keep the state for all of them. - -State is basically like a global object that is available everywhere in a component. +State is a feature that is only available to classes, and should be considered private data. State is only available within a component, and can be passed to child components using [`props`](../props) Example of a Stateful Class Component: -```javascript +```js import React from 'react'; class App extends React.Component { constructor(props) { super(props); - // We declare the state as shown below - + // We declare state below this.state = { x: "This is x from state", y: "This is y from state" @@ -38,64 +35,65 @@ class App extends React.Component { export default App; ``` -Another Example: +Another example using class properties and ES6 destructring: + +#### Note: This example uses class fields which is a stage 3 proposal and is not a standard feature yet. ```javascript import React from 'react'; - class App extends React.Component { - constructor(props) { - super(props); - - // We declare the state as shown below - this.state = { - x: "This is x from state", - y: "This is y from state" - } + state = { + fruits: ['apple', 'banana', 'strawberry', 'watermelon'] } render() { - let x1 = this.state.x; - let y1 = this.state.y; + // Destructure `fruits` property from this.state + const { fruits } = this.state return ( -