React Native DatePickerIOS旋转回旧日期

时间:2018-06-18 07:17:24

标签: ios objective-c react-native datepicker

我在React原生中使用DatePickerIOS,在DatePickerIOS的更改日期之后轮子旋转回旧日期,但在警告显示新日期。

我想设置新日期并删除Wheel Rotate Back to old Date Issue。

import React, {Component} from 'react';
import {StyleSheet, View, DatePickerIOS, Text} from 'react-native';
import { StackNavigator } from 'react-navigation';

var moment = require('moment');

const DatePickerIOSExample = (props) => {

    state = {
        stateDate: new Date(),
    }

    changeDate = (date) => {
        this.setState = ({
            stateDate:date
        })
        alert(this.setState.stateDate);
    }

    const {navigate} = props.navigation;
    return(
        <View>
            <DatePickerIOS 
                date={this.state.stateDate} 
                onDateChange={this.changeDate.bind(this)} 
                mode="date" />

            <Text>{moment(this.state.stateDate).format('DD/MM/YYYY')}</Text>
        </View>
    );
}

export default DatePickerIOSExample;

1 个答案:

答案 0 :(得分:1)

你是在abject&#34; setState&#34;而不是调用setState。

正确的方法是:

changeDate = (date) => {
    this.setState({
        stateDate:date
    })
    alert(this.state.stateDate);
}

正如您所看到的,我删除了&#34; =&#34;在&#34; setState&#34;之后我已经更改了提醒内容&#34; this.state.stateDate&#34; (所以你正在访问该州)。

重要提示:请记住setState是异步的(https://medium.com/@wereHamster/beware-react-setstate-is-asynchronous-ce87ef1a9cf3