This is a question related to the react native set state.

Asked 1 years ago, Updated 1 years ago, 96 views

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

class RECORD extends Component{

    constructor(props){
        super(props)

        this.state={
                results:null
        }

        axios.get('http://beam1100.cafe24app.com/record')
        .then((res)=>{
            this.setState({results:res.data.results})   
        })


    }

    render(){
        console.log(this.state.)
        return(
            <View>
                <Text>Test page.</Text>
            </View>
        )
    }
}

export default RECORD

Put the array taken from DB in the constructor mode in the state

I'd like to take care of it at Lander Massad.

However,

When outputting results from the render function, null is output and an array with values is output shortly.

How can I print out synchronously at once?

react-native

2022-09-21 10:17

1 Answers

I found it while I was googling.

React does not re-render the DOM every time the state is changed. For performance efficiency, if there are set states several times, the DOM is re-rendered at the most appropriate time for React to determine by integrating changes in other states at once. Therefore, the basic way of operating setState is Asynchronous.

handleClick = () => {
    this.setState({meaningOfLife: this.state.meaningOfLife + 1});
    console.log(this.state.meaningOfLife);
  }

For example, a method called handleClick uses the setStateLife method to add the state value of meaningOfLife by 1. Even if you log console.log immediately after executing the above code, the number is always recorded one beat later than shown in the DOM. Because setState is executed asynchronously, the time when console.log is executed is still before the state is changed by setState. Particularly noteworthy in the above code is that the first factor in setState is meaningOfLife: this.state.meaningOfLife + 1. The value of the state to be changed is set based on the value of the current this.state...

From:https://soldonii.tistory.com/112


2022-09-21 10:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.