freeCodeCamp/guide/english/react-native/state/index.md

81 lines
2.2 KiB
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Component State
---
2018-10-12 19:37:13 +00:00
## Component State
In `Class` components, there is a way to store and manage state built in to React Native.
```javascript
class App extends Component {
constructor() {
2018-10-12 19:37:13 +00:00
super();
this.state = {
counter: 0,
2018-10-12 19:37:13 +00:00
};
}
incrementCount() {
2018-10-12 19:37:13 +00:00
this.setState({
counter: this.state.counter + 1,
2018-10-12 19:37:13 +00:00
});
}
decrementCount() {
2018-10-12 19:37:13 +00:00
this.setState({
counter: this.state.counter - 1,
2018-10-12 19:37:13 +00:00
});
}
render() {
2018-10-12 19:37:13 +00:00
return (
<View>
<Text>Count: {this.state.counter}</Text>
<Button onPress={this.decrementCount.bind(this)}>-</Button>
<Button onPress={this.incrementCount.bind(this)}>+</Button>
</View>
);
}
}
```
State is similar to props, but it is private and fully controlled by the component. Here, the `constructor()` method is calling the parent class' constructor with `super();` - **`Component`** is the parent class of `App` because we are using the `extends` keyword. The `constructor()` method also initializes the component's state object:
```js
2018-10-12 19:37:13 +00:00
this.state = {
counter: 0,
2018-10-12 19:37:13 +00:00
};
```
The state can be displayed within the component:
```js
{
this.state.counter;
}
2018-10-12 19:37:13 +00:00
```
Or updated by calling:
```js
this.setState({});
```
**Note:** Aside from its initial creation in your component's `constructor()` method, you should never directly modify the component's state with `this.state =`. You must use `this.setState`, as shown in the `incrementCount` and `decrementCount` functions above.
2018-10-12 19:37:13 +00:00
The count is incremented and decremented by calling the functions passed to the `onPress` handlers, just like they would if you called a click handler from JavaScript on the web.
2018-10-12 19:37:13 +00:00
_ASIDE: In the first example, `<Button>` is a custom component; it's a combination of `<TouchableOpacity>` and `<Text>` from the React Native API:_
2018-10-12 19:37:13 +00:00
```js
const Button = ({ onPress, children, buttonProps, textProps }) => {
const { buttonStyle, textStyle } = styles;
return (
<TouchableOpacity onPress={onPress} style={[buttonStyle, buttonProps]}>
<Text style={[textStyle, textProps]}>{children}</Text>
2018-10-12 19:37:13 +00:00
</TouchableOpacity>
);
};
```
#### More Information
- [Good article about state and props](https://learnreact.design/2017/08/16/components-props-and-state)