React Native RN state props

state:  RN is specially used to identify whether to re-render, and update data through the value of the attribute. React will monitor the change of state internally. Once there is a change, it will actively trigger the render() method of the component to update the Dom structure. In addition, the state is the component Private, there is no way to pass it through other components.

import React, {Component} from 'react';
import { Text, View } from 'react-native';

export default class App extends Component {
    state = {
        myState: 'This is a text component, created using state data. It will change or updated on clicking it.'
    }
    updateState = () => this.setState({myState: 'The state is updated'})
    render() {
        return (
            <View>
                <Text onPress={this.updateState}> {this.state.myState} </Text>
            </View>
        );
    }
}
    this.setState({
      buttonText: value,
      selectedIndex: idx,
    });

The setState() method will add the changes to the component state to the queue and tell React that the component and its subcomponents need to be re-rendered. React does not guarantee that the state will be updated as soon as setState() is called. Therefore, if the value of this.state is read immediately after calling setState(), errors may occur, so the second parameter callback of the setState(updater, callback) method is an optional parameter, which has not been found yet. instance.

reference

ReactNative from zero to complete project-detailed use of state- Nuggets

FlatList Learning Records in React-Native (2) Single Item Refresh_flatlist Partial Refresh_Neighbor Lao He's Blog-CSDN Blog

 

Guess you like

Origin blog.csdn.net/linzhiji/article/details/131903098