react-native                
            状態
        
        
            
    サーチ…
構文
- void setState(function | nextStateオブジェクト、[function callback])
 
setState
アプリケーションのビューを変更するには、 setStateを使用します。これにより、コンポーネントとその子コンポーネントが再描画されます。 setStateは、新しい状態と以前の状態との間の浅いマージを実行し、コンポーネントの再レンダリングをトリガします。 
 setStateは、キー値オブジェクトまたはキー値オブジェクトを返す関数のいずれかをとります。 
Key-Valueオブジェクト
this.setState({myKey: 'myValue'});
 関数
関数を使用すると、既存の状態または小道具に基づいて値を更新するのに便利です。
this.setState((previousState, currentProps) => {
    return {
        myInteger: previousState.myInteger+1
    }
})
 また、コンポーネントが新しい状態で再レンダリングされたときにsetStateするオプションのコールバックをsetState渡すこともできます。 
this.setState({myKey: 'myValue'}, () => {
    // Component has re-rendered... do something amazing!
));
 完全な例
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, TouchableOpacity } from 'react-native';
export default class MyParentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      myInteger: 0
    }
  }
  getRandomInteger() {
    const randomInt = Math.floor(Math.random()*100);
    this.setState({
      myInteger: randomInt
    });
  }
  incrementInteger() {
    
    this.setState((previousState, currentProps) => {
      return {
        myInteger: previousState.myInteger+1
      }
    });
  }
  render() {
    return <View style={styles.container}>
      
      <Text>Parent Component Integer: {this.state.myInteger}</Text>
      <MyChildComponent myInteger={this.state.myInteger} />
      <Button label="Get Random Integer" onPress={this.getRandomInteger.bind(this)} />
      <Button label="Increment Integer" onPress={this.incrementInteger.bind(this)} />
    </View>
  }
}
export default class MyChildComponent extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    
    // this will get updated when "MyParentComponent" state changes
    return <View>
      <Text>Child Component Integer: {this.props.myInteger}</Text>
    </View>
    
  }
}
export default class Button extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return <TouchableOpacity onPress={this.props.onPress}>
        <View style={styles.button}>
          <Text style={styles.buttonText}>{this.props.label}</Text>
        </View>
      </TouchableOpacity>
    
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  button: {
    backgroundColor: '#444',
    padding: 10, 
    marginTop: 10
  },
  buttonText: {
    color: '#fff'
  }
});
AppRegistry.registerComponent('MyApp', () => MyParentComponent);
        状態を初期化する
次のように、コンポーネントのコンストラクタ関数内で状態を初期化する必要があります。
export default class MyComponent extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
      myInteger: 0
    }
  }
  render() {
    return  (
      <View>
        <Text>Integer: {this.state.myInteger}</Text>
      </View>
    )
  }
}
 setStateを使用すると、ビューを更新できます。
Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow