zahra
5 سال پیش توسط zahra مطرح شد
0 پاسخ

ذخیره نکردن اطلاعات در AsyncStorage

دوستان کسی می دونه مشکل این چیه؟
من یه تقویم دارم که هر روز یه اطلاعاتی ذخیره می کنه و هر روز با روزدیگه فرق می کنه
الان این اساین استورج من جواب نمیدعه و اطلاعات یه روز ذخیره نمی کنه بعد که دوباره برنامه را باز می کنم اطلاعاتی ذخیره نشده


import Note from './Note';

export default class Info extends Component {
    constructor(props) {
        super(props);
        const {navigation} = this.props;
        this.currentDate = navigation.getParam('currentDate', "0001/01/01");
        this.state = {
            noteText:'',
            noteArray:[]
        };
    }

    static navigationOptions = {header : null };
    componentDidMount = () => {
        AsyncStorage.getItem('user:event', (error, result) => {
            let data = [];
            if (result) {
                data = JSON.parse(result);
            }
            let event = data.find(item => item.date === this.currentDate);
            if (event) {
                this.setState({
                    noteText: event.noteText,
                    noteArray:event.noteArray
                });
            }
        });
    };

    //-----------

    currentTime() {
        var d = new Date();
        var h = d.getHours();
        var m = d.getMinutes();
        return h + ":" + m  ;
    }

    addNote(){
        if(this.state.noteText){
            this.state.noteArray.push({
                'time':this.currentTime(),
                'note':this.state.noteText
            });
            this.setState({
                noteArray:this.state.noteArray,
                noteText:''
            })
        }
    }

    deleteNote(key){
        this.state.noteArray.splice(key,1);
        this.setState({
            noteArray: this.state.noteArray
        });
    }
    //-----------

    render() {
        let notes=this.state.noteArray.map((val,key)=>{
            return <Note key={key} keyVal={key} val={val}deleteMethod={()=>this.deleteNote(key)} source={this.state.noteText}/>
        })
        return (
            <View style={styles.container}>
                <Button rounded light style={{backgroundColor: '#1874c3'}} onPress={this.saveData}>
                    <Text style={{color: '#fcf41f', fontFamily: 'IRANSansMobile_Bold'}}> ذخيره اطلاعات </Text>
                </Button>
                <ScrollView style={styles.scrollStyle}>
                    {notes}
                </ScrollView>

                <View style={styles.footer}>
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="یادداشت"
                        placeholderTextColor="white"
                        underlineColorAndroid="transparent"
                        value={this.state.noteText}
                        onChangeText={(value)=>this.setState({noteText:value})}
                    />
                </View>
                <TouchableOpacity style={styles.addButton} onPress={this.addNote.bind(this)}>
                    <Text style={styles.addButtonText}>+</Text>
                </TouchableOpacity>
            </View>
        );
    }

    saveData = () => {
        AsyncStorage.getItem('user:event', (error, result) => {
            let data = [];
            if (result) {
                data = JSON.parse(result);
            }
            let event = data.find(item => item.date === this.currentDate);
            if (event) {
                event.noteText = this.state.noteText;
                event.noteArray = this.state.noteArray;

            } else {
                data.push({
                    noteText: this.state.noteText,
                    noteArray: this.state.noteArray

                });
            }
            AsyncStorage.setItem('user:event', JSON.stringify(data));
            Toast.show({
                text: 'اطلاعات شما با موفقيت براي تاريخ ' + this.currentDate + ' ذخيره شد',
            });
            this.props.navigation.goBack();
        });
    };
}

اینم کدهای تقویمم


export default class Main extends Component<Props> {
    constructor(props) {
        super(props);
        this.state = {
            selectedStartDate: null,
        };
        this.onDateChange = this.onDateChange.bind(this);}
    onDateChange(date) {
        let currentDate = date.format('jYYYY/jM/jD');
        this.props.navigation.navigate('Info', {currentDate: currentDate});
    }
    static navigationOptions = {header : null };
    render() {
        const {navigate}=this.props.navigation;
        return (
            <View style={{backgroundColor: '#FFF8E1',flex:1}}>
                <View style={styles.container}>
                    <PersianCalendarPicker
                        onDateChange={this.onDateChange}
                    />
                </View>
            </View>
        );
    }
}

ثبت پرسش جدید

به همدیگه کمک کنیم

به zahra کمک کنید تا مشکل خودش را حل کند؛ این‌طور می‌توانیم با هم پیشرفت کنیم.

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام