State trong RN là gì?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
474 lượt xem
  • Các component React có một đối tượng state được tích hợp sẵn.
  • Đối tượng state là nơi bạn lưu trữ các giá trị thuộc tính thuộc về component đó.
  • Khi đối tượng state thay đổi, component sẽ re-render.
import React, { Component } from 'react';
import { Text, View } from 'react-native';

class Blink extends Component {

   componentDidMount(){
      // Toggle the state every second
      setInterval(() => (
         this.setState(previousState => (
            { isShowingText: !previousState.isShowingText }
         ))
      ), 1000);
   } 

   //state object
   state = { isShowingText: true };

   render() {
      if (!this.state.isShowingText) {
         return null;
      } 
      
      return (
         <Text>{this.props.text}</Text>
      );
   }
} 

export default class BlinkApp extends Component {
   render() {
      return (
         <View>
            <Blink text='I love to blink' />
            <Blink text='Yes blinking is so great' />
            <Blink text='Why did they ever take this out of HTML' />
            <Blink text='Look at me look at me look at me' />
         </View>
      );
   }
}
{{login.error}}