javascript - React Native,成功设置状态但想同时保存在asyncStorage中

标签 javascript android reactjs react-native mobile

我有一个用于跟踪健康统计信息的 React native 应用程序模块。这个例子是关于重量的,但是当仪表板加载时,有一个空组件,带有一个按钮来触发编辑器模式。这提供了一个 textInput,用户可以在其中输入他们当天的体重,并且在保存时将该数字保存到该值的状态,现在它显示在仪表板上。

这完全符合预期,但我还想将其与当前日期同时保存在 asyncStorage 中。如果当前日期与它的存储值匹配,我想加载这些值的状态,但要保存它一整天。

基本上,如果用户在星期一醒来并记录他们的体重,我希望它整天都显示在应用程序中,但是当他们星期二打开它时,它应该再次为空,以便他们在那天输入,然后值应在星期二等整天保持不变。

我需要做什么才能实现这一目标?

import React, { Component } from 'react';
import { StyleSheet, Text,  View, Dimensions, Button, ScrollView, TouchableOpacity, TextInput, Alert} from 'react-native';
import { Pedometer } from "expo-sensors";
import {
  LineChart,
  BarChart,
  PieChart,
  ProgressChart,
  ContributionGraph,
  StackedBarChart
} from "react-native-chart-kit";
import PedometerChart from './PedometerChart';
import Icon from '@expo/vector-icons/Ionicons';
import MatIcon from '@expo/vector-icons/MaterialCommunityIcons';
import NumericInput from 'react-native-numeric-input';
import Modal from 'react-native-modal';
import moment from 'moment';



export default class Dashboard extends Component{

    state = {
    weight:'',
    currentDate: new Date(),
    markedDate: moment(new Date()).format("YYYY-MM-DD")
  };

  render() {

    const today = this.state.currentDate;
    const day = moment(today).format("dddd");
    const date = moment(today).format("MMMM D, YYYY");

    return(
      <ScrollView>
      <View style={styles.container}>

      <View style={styles.mainContainer}>
        <View style={styles.widgetContainer}>
            <MatIcon  name="scale-bathroom" size={50} />
            <Text style={styles.titleText}>Weight</Text>
        </View>
        <View style={styles.widgetContainer}>
            <Text>
              <Text style={styles.textUnderlines}>
              {this.state.weight}
              </Text>
              <Text style={styles.recordsTypes}>
              lb
              </Text>
            </Text>
            <TouchableOpacity 
                style={styles.recordButton} 
                onPress={this.toggleWeightModal}
            >
            <Text style={styles.recordText}>Record</Text>
            </TouchableOpacity>
        </View>
      </View>

      <Modal 
          isVisible={this.state.isWeightModalVisible}
          backdropOpacity={0.7}
          onBackdropPress={() => this.setState({ isWeightModalVisible: false })}
      >
        <View style={{ flex: 1 }}>
          <View style={styles.recordingModals}>
          <Text style={styles.titleText}>Weight for {date}</Text>
          <TextInput 
              value={this.state.weight}
              onChangeText={(weight) => this.setState({ weight })}
              keyboardType="numeric" 
              underlineColorAndroid="#000" 
              style={styles.modalInput} 
              placeholder="lbs"/>
          <TouchableOpacity style={styles.recordButtonModal} onPress={this.toggleWeightModal}>
          <Text style={styles.recordText}>Save Entry</Text>
          </TouchableOpacity>
          </View>
        </View>
      </Modal>
      </ScrollView>
    ) ;
  }
}

最佳答案

可以获取今天的日期并将其存储在变量中。如果日期不相同,则比较该变量和来自状态(this.state.today)的日期,空重量容器。

  1. 当重量容器更新时,更新日期变量,这样它就不会在同一天再次询问重量 如果应用程序一直运行,则在每天晚上 12 点重置重量容器,没有任何循环

关于javascript - React Native,成功设置状态但想同时保存在asyncStorage中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369694/

相关文章:

javascript - Foundation <nav data-topbar> 不允许将 <section> 写入 <div>

javascript - 无限的 Google Places 实例

android - Canvas.drawText() 不起作用

android - 在android中放大/缩小或拖动后获取Canvas坐标

reactjs - React 复选框未在功能组件中更新

javascript - 尝试使用 Marak/faker.js 导入数据时出错

javascript - 更新期间控制复选框的最佳方法 | ReactJS

javascript - AngularJS 中的进度处理程序

android - 调试android数据绑定(bind)?

Javascript 回调不起作用,在 ajax 请求完成之前运行回调