How to use state and setState in react (detailed tutorial)

亚连
Release: 2018-06-22 14:21:39
Original
3280 people have browsed it

This article mainly introduces the use of state and setState in react study notes. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

In react, state and setState() are used to control the state of components.

state

state is used in react to store component data status, which can be compared to data in vue.

1. The role of state

state is an object of a component in React. React treats the user interface as a state machine, imagines that it has different states and then renders these State can easily keep the user interface consistent with the data.

In React, updating the state of a component will cause the user interface to be re-rendered (do not operate the DOM). Simply put, the user interface will change with the state And change.

2. How state works

The commonly used method to notify React of data changes is to call setState(data, callback). This method will merge data into this .state, and re-render the component. After rendering is completed, call the optional

callback. In most cases, there is no need to provide a callback, because React will be responsible for updating the interface to the latest state.

setState()

The difference from vue is that state cannot be modified directly and needs to be modified through the setState() method.

1. SetState() will not take effect immediately after updating the component state. In order to improve performance, react will update the state in batches and then render, which is an asynchronous operation. Therefore, it is not necessary to obtain the value of state immediately after setSate(). The status after the update.

2. The first parameter of setState() accepts two types of parameters, Object and Function

Object

this.setState({ msg: '更新state msg' })
Copy after login

When the parameter is Object At this time, it can be the key in the corresponding state, and the value is the new value.

Function

When the parameter is a function, setState() will pass the result of the previous setState() as a parameter to this function

... constructor () { this.state = { counter: 0 } } add() { this.setState({ counter: this.state.counter + 1 }) this.setState({ counter: this.state.counter + 1 }) // 此时`this.state.counter`的值还是初始值0,,所以这个操作是无效的 this.setState(prevState => { counter: prevState.counter + 1 }) // `prevState.counter` 为上次更新之后的值,即是1 } ...
Copy after login

setState() The second parameter is a callback function, indicating that the state update is completed

this.setState({ msg: '更新state msg' }, () => { console.log('state 更新完毕') })
Copy after login

According to this, you can use Promise and async/await to encapsulate it into a synchronous operation

setStateAsync(state) { return new Promise(resolve => { this.setState(state, resolve) }) } // 使用 async add() { await setStateAsync({ counter: this.state.counter + 1 }) console.log('state 更新完毕') }
Copy after login

The above is what I compiled Everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to use the swiper component to achieve image switching display in WeChat Mini Program

What are there in vue2.0 Commonly used UI libraries?

How to convert a character into an integer in C

#How to merge vue2.0 and animate.css together (details Tutorial)

The above is the detailed content of How to use state and setState in react (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!