Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 来自包的异步存储"@react本机社区/异步存储”;在react native中未正确检索日期_React Native_Date_Storage - Fatal编程技术网

React native 来自包的异步存储"@react本机社区/异步存储”;在react native中未正确检索日期

React native 来自包的异步存储"@react本机社区/异步存储”;在react native中未正确检索日期,react-native,date,storage,React Native,Date,Storage,我在react native中有以下代码: const handleRefresh = useCallback(async () => { const date = await AsyncStorage.getItem(`lastRequestDate`); console.log('date: ', date); console.log('type of: ', typeof date === 'string' ? date : ''); const previo

我在react native中有以下代码:

 const handleRefresh = useCallback(async () => {
  const date = await AsyncStorage.getItem(`lastRequestDate`);

  console.log('date: ', date);

  console.log('type of: ', typeof date === 'string' ? date : '');

  const previousDate = new Date(typeof date === 'string' ? date : '');
  // const previousDate = new Date('2021-06-02T00:52:46.892Z');

  console.log('previous date: ', previousDate);
  
}, [user]);

当我控制台日志previousDate时,我得到
Date{NaN}
但如果我控制台日志Date时,我得到
“2021-06-02T00:52:46.892Z”
。如果控制台日志
const previousDate=新日期('2021-06-02T00:52:46.892Z')我得到了正确的日期。但是,如果我将字符串替换为date变量,则会出现错误(
date{NaN}
)(
const-previousDate=new-date(typeof-date=='string'?date:);

尝试将条件保存在变量中,并在
新日期(…)中使用该条件。

const newDate=typeof date=='string'?日期:''
const previousDate=新日期(newDate);