前提・実現したいこと
ボタンを押したら、その押した回数をAsyncStorageに保存したいと考えています。
発生している問題
ボタンを押すと'{this.state.applause}'で表示されている数字は1ずつ増えていくのですが
SimulatorをRefreshすると、「0」になってしまいます。
お聞きしたいこと
Refreshして「0」になっているということはAsyncStoregeに保存がされてないと思うのですが
AsyncStorageに保存し、続きからの数字を表示するためにはどこを修正すれば良いでしょうか?
アドバイスいただけたら幸いです。 よろしくお願いいたします。
該当のソースコード
JS
1export default class ApplauseButton extends Component { 2 constructor(props) { 3 super(props); 4 this.state = { 5 applaused: 0, 6 }; 7 } 8 9 componentDidMount = () => { 10 AsyncStorage.getItem('applause'); 11 }; 12 13 handlClick() { 14 AsyncStorage.setItem('applause', this.state.applause.toString()).then(() => { 15 this.setState({ applause: this.state.applause + 1}); 16 }); 17 }; 18 19 render() { 20 return ( 21 <View style={styles.container}> 22 <Button title="????" 23 onPress={() => { 24 this.handlClick() 25 }} /> 26 <Text style={styles.count}> 27 {this.state.applause} 28 </Text> 29 </View> 30 ); 31 } 32}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/15 07:26