TypeError: undefined is not a function (near ‘…markedDates.push…’)

huangapple go评论72阅读模式
英文:

TypeError: undefined is not a function (near '...markedDates.push...')

问题

当我使用React Native开发应用时,出现了以下问题。

> TypeError: undefined is not a function (near '...markedDates.push...')

我试图将以下数据放入markedDates中。

{'2023-03-03': {marked: true, dotColor: 'blue'}}
然而,我搜索了很多信息,但找不到与我的情况相符的情况。

这是我的代码。
import React from 'react';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {Calendar} from "react-native-calendars";
import {View, StyleSheet} from 'react-native';

function HomeScreen({navigation}) {
  var dateString = "";
  var markedDates = {
    '2023-02-01': {marked: true, dotColor: 'red'},
  }

  markedDates.push({'2023-03-03': {marked: true, dotColor: 'blue'}});
  return(
      <SafeAreaProvider>
         <View>
            <Calendar
              markedDates={markedDates}
              onDayPress={(day) => {
                
              }}
           />
         </View>
      </SafeAreaProvider>
  );
}

export default HomeScreen;

这个问题是否有人可以解决?如果可以的话,请帮助我。谢谢。

英文:

When I was developing an app using React Native, the following problem occurred.

> TypeError: undefined is not a function (near '...markedDates.push...')

I'm trying to put the following data into markedDates.

{&#39;2023-03-03&#39;: {marked: true, dotColor: &#39;blue&#39;}}

However, I searched a lot of information, but I couldn't find a situation that corresponds to my situation.

This is my code.

import React from &#39;react&#39;;
import {SafeAreaProvider} from &#39;react-native-safe-area-context&#39;;
import {Calendar} from &quot;react-native-calendars&quot;;
import {View, StyleSheet} from &#39;react-native&#39;;

function HomeScreen({navigation}) {
  var dateString = &quot;&quot;;
  var markedDates = {
    &#39;2023-02-01&#39;: {marked: true, dotColor: &#39;red&#39;},
  }

  markedDates.push({&#39;2023-03-03&#39;: {marked: true, dotColor: &#39;blue&#39;}});
  return(
      &lt;SafeAreaProvider&gt;
         &lt;View&gt;
            &lt;Calendar
              markedDates={markedDates}
              onDayPress={(day) =&gt; {
                
              }}
           /&gt;
         &lt;/View&gt;
      &lt;/SafeAreaProvider&gt;
  );
}

export default HomeScreen;

Is there anyone who can solve this problem? If so, please help me. Thank you.

答案1

得分: 1

markedDates 被初始化为一个对象,而不是一个数组。因此,你不能在它上面使用 push() 方法。

英文:

markedDates is initialized as an object, not an array. Therefore, you cannot use the push() method on it.

huangapple
  • 本文由 发表于 2023年3月3日 18:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定