我有多个柜台;页面刷新时,所有计数都会更新我想将它们另存为其他计数器

问题描述

我正在渲染多个计数器。它们的值会保存,但最后单击的计数器会将其值转到所有其他计数器。我想分别保存每个值。

我不确定如何执行此操作。此外,即使删除了其父项,该计数也不会删除。

const {width,height} = Dimensions.get('window')


 class CounterButton extends Component {
  constructor(props){
    super(props);
    this.state = {
      clicks: 0,};
  }
  // const [ounter,setCounter]=useState(0);


componentDidMount = async() => {
  
    let count = '';
    try {
      count = await AsyncStorage.getItem('clicks') || '0';
       count = parseInt(count,0);
       this.setState({clicks:count})
    } catch (error) {
      // Error retrieving data
      console.log(error.message);
    }
  };
  
handleadd= async ()=> {
  const count = this.state.clicks + 1;
 try{
 await AsyncStorage.setItem('clicks',count.toString());
    this.setState({ clicks: count});
}
catch(error){
 console.log(error.message);
}
};
handleminus= async ()=> {
  
  const count = this.state.clicks - 1;
  
 try{
 await AsyncStorage.setItem('clicks',count.toString());
    this.setState({ clicks: count});
}
catch(error){
 console.log(error.message);
}
};
 




render(){
  return (
      <View style={styles.container}>
       
        
           {/* <Counter
            value={state2}
            onValueChange={setState2}
            minValue={0}
            activeBackgroundColor="#E6EFFF"
            buttonStyle={{
              backgroundColor: '#008080',paddingVertical: 7,}}
            buttonnTextStyle={{
              color: 'white',fontSize: 20,paddingHorizontal: 5,}}
            labelStyle={{
              backgroundColor: '#ffffff',}}
            labelTextStyle={{
              color: '#008080',}}
          />  */}
          
      <Text>You clicked {this.state.clicks} times</Text>
      <TouchableHighlight onPress={() => {this.handleadd()}}>
        <Text>+</Text>   
       </TouchableHighlight>
       <TouchableHighlight onPress={() => {this.handleminus()}}>
        <Text>-</Text>   
       </TouchableHighlight>

          
        </View>
  );
};


}

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',right:'30%'
    
  },});
export default CounterButton;

计数器在平面列表中呈现。当我不使用异步时,它们的值是分开的,但是现在我保存了,它们的值就变得一样了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...