0

I am getting this error

TypeError: null is not an object(evaluating 'dataSource.map')

I have looked at other solutions, but I don't understand why setDatasource does not change dataSource from null.

import React, { Component, useState, useEffect } from "react";
    import { View, Text, StyleSheet, ActivityIndicator } from "react-native";
    import { ScreenContainer } from "react-native-screens";

    export const Home = () => {
      const [isLoading, setisLoading] = useState(true);
      const [dataSource, setdataSource] = useState(null);

      useEffect(() => {
        return fetch("https://facebook.github.io/react-native/movies.json")
          .then((response) => response.json())
          .then((responseJson) => {
            setisLoading(false), setdataSource(responseJson.movie);
          });

        // .catch((error)) => {
        //   console.log(error)
        // }
      });
      if (isLoading) {
        return (
          <View>
            <ActivityIndicator />
          </View>
        );
      } else {
        let moviest = dataSource.map((val, key) => {
          return (
            <View key={key}>
              <Text>{val.title}</Text>
            </View>
          );
        });

        return (
          <ScreenContainer style={styles.container}>
            <View>
              <Text>These are the movies</Text>
              {moviest}
            </View>
          </ScreenContainer>
        );
      }
    };
Jay Alli
  • 49
  • 6

1 Answers1

1

You can use collections.Counter (doc) for the task:

l = [28, 24, 15, 20, 11, 17, 20, 12, 12, 15]

from collections import Counter

c = Counter(l)    
out = [k for k, v in c.items() if v > 1]

print(out) # or print(sorted(out)) for printing the list sorted

Prints:

[15, 20, 12]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91