关闭应用程序后,React Native进度条百分比不保持不变

React Native progress bar percentage not staying the same after closing the app

本文关键字:百分比 应用程序 Native React      更新时间:2023-09-26

每次我按下一个按钮,进度条将上升%20,但问题是,例如,如果它在%80,我重新加载应用程序,它会显示为%60,而不是,我想知道是否有人可以帮助我解决这个问题,使进度条百分比保持不变后重新加载或关闭应用程序。

'use strict';
var React = require('react-native');
var ProgressBar = require('react-native-progress-bar');
var {
  AppRegistry,
  AsyncStorage,
  StyleSheet,
  Text,
  View,
  TouchableHighlight
} = React;
var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFF',
  },
  button: {
    alignSelf: 'center',
    marginTop: 50,
    width: 100,
    height: 50,
    backgroundColor: '#0059FF',
    borderRadius: 8,
    borderWidth: 2,
    borderColor: '#0059FF'
  },
  buttonClear: {
    alignSelf: 'center',
    marginTop: 10,
    width: 100,
    height: 50,
    backgroundColor: '#3B3A3A',
    borderRadius: 8,
    borderWidth: 2,
    borderColor: '#3B3A3A'
  },
  buttonText: {
    fontSize: 18,
    textAlign: 'center',
    lineHeight: 33,
    color: '#FFF',
  }
});
var PROGRESS = 0;
class BasicStorageExample extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      progress: PROGRESS
    }
  }
  componentDidMount() {
    AsyncStorage.getItem('progressbar')
      .then((value) => {
        JSON.parse(value);
        this.setState({
          progress: value 
        });
        console.log('Progress on load: ' + value);
      })
      .done();
  }
  onButtonPress() {
    AsyncStorage.setItem('progressbar', JSON.stringify(PROGRESS))
      .then(() => {
        JSON.parse(PROGRESS);
        this.setState({
          progress: PROGRESS += 0.2
        });
        console.log('Progress on Button Press: ' + PROGRESS);
      })
      .done();
  }
  onButtonClearPress() {
    AsyncStorage.setItem('progressbar', JSON.stringify(PROGRESS))
      .then(() => {
        JSON.parse(PROGRESS);
        PROGRESS = 0;
        this.setState({
          progress: 0
        });
      })
      .done();
  }
  render() {
    return (
      <View style={styles.container}>
        <ProgressBar
          fillStyle={{}}
          backgroundStyle={{backgroundColor: '#cccccc', borderRadius: 2}}
          style={{marginTop: 10, width: 300}}
          progress={this.state.progress} />
        <TouchableHighlight
          ref="button"
          style={styles.button}
          underlayColor='#002C7F'
          onPress={this.onButtonPress.bind(this)}>
          <Text style={styles.buttonText}>Done</Text>
        </TouchableHighlight>
        <TouchableHighlight
          style={styles.buttonClear}
          underlayColor='#002C7F'
          onPress={this.onButtonClearPress.bind(this)}>
          <Text style={styles.buttonText}>Clear</Text>
        </TouchableHighlight>
      </View>
    );
  }
};
AppRegistry.registerComponent('BasicStorageExample', () => BasicStorageExample);

问题是您正在获得PROGRESS的旧值(在您增加0.2之前)并将此值设置为项目progressbar

所以,每次你重新加载时,React运行componentDidMount函数,并且因为你将progressbar设置为PROGRESS的旧值,它将始终显示它比你在视图中看到的增加一个。

试着改变你的onButtonPress():

onButtonPress() {
    PROGRESS += 0.2;
    AsyncStorage.setItem('progressbar', JSON.stringify(PROGRESS))
    ... // continues normally