Not sure how to include function in useEffect

I am trying to mount a function within useEffect, how would I call this function within useEffect, I am not sure?
Initially useEffect is written as following in the app-

    useEffect(() => {
    CognitensorEndpoints.getDashboardList({
      dispatchReducer: dispatchDashboards,
    });
    CognitensorEndpoints.getList({
      dispatchReducer: dispatchDashboards,
    });
  },[]); 

Then when I include useEffect as below, it returns errors (‘Cannot read property ‘filter’ of undefined’) –

    useEffect(() => {
    console.log('abcd');
    setLoading();
    CognitensorEndpoints.getDashboardList({
      dispatchReducer: dispatchDashboards,
    });
    CognitensorEndpoints.getList({
      dispatchReducer: dispatchDashboards,
    });
  },[]);

How do I correctly include ‘setLoading()’ within useEffect?

Here’s the full app code-

import React, { useState, useEffect, useReducer } from 'react';
import { View, Text, StyleSheet, FlatList, ActivityIndicator, Keyboard, Button } from 'react-native';
import { Searchbar } from 'react-native-paper';
import { theme } from '../theme';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { apiStateReducer } from '../reducers/ApiStateReducer';
import CognitensorEndpoints from '../services/network/CognitensorEndpoints';
import DefaultView from '../components/default/DefaultView';
import DashboardListCard from '../components/DashboardListCard';
import DashboardHeader from '../components/DashboardHeader';
import DashboardGridCard from '../components/DashboardGridCard';
import {
  NavigationContainer,
  useFocusEffect,
} from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

const AppHeader = ({
  scene,
  previous,
  navigation,
  searchIconVisible = false,
  item,
  index,
  onPress
}) => {
  const [dashboards, dispatchDashboards] = useReducer(apiStateReducer, {
    data: [],
    isLoading: true,
    isError: false,
  });
  const [gridView, setGridView] = useState(false);

  const toggleGridView = () => {
    setGridView(!gridView);
  };

  const [filtered, setFiltered] = useState([]);

  const setLoading = () => {
  const messages = dashboards.data.message.filter((item) => {
      const title = item.dashboardTitle || item.dashboardName;
      return title.toLowerCase();
    });
    setFiltered(messages);
    console.log(filtered);
  };

  const dropShadowStyle = styles.dropShadow;
  const toggleSearchVisibility = () => {
    navigation.navigate('Search');
  };

  useEffect(() => {
    console.log(abcd);
    setLoading();
    CognitensorEndpoints.getDashboardList({
      dispatchReducer: dispatchDashboards,
    });
    CognitensorEndpoints.getList({
      dispatchReducer: dispatchDashboards,
    });
  },[]);

  return (
    <>
    <View style={styles.header}>
      <View style={styles.headerLeftIcon}>
        <TouchableOpacity onPress={navigation.pop}>
          {previous ? (
            <MaterialIcons
              name="chevron-left"
              size={24}
              style={styles.visible}
            />
          ) : (
            <MaterialIcons
              name="chevron-left"
              size={24}
              style={styles.invisible}
            />
            )}
        </TouchableOpacity>
      </View>
      {filtered.map(item => (
      <Text style={styles.headerText}>
        {item.dashboardTitle}
      </Text>
    ))}
      <View style={styles.headerRightIconContainer}>
        {searchIconVisible ? (
          <TouchableOpacity
            style={[styles.headerRightIcon, dropShadowStyle]}
            onPress={toggleSearchVisibility}>
            <MaterialIcons name="search" size={24} style={styles.visible} />
          </TouchableOpacity>
        ) : (
          <View style={styles.invisible} />
        )}
      </View>
    </View>
  </>
  );
}; 

17 thoughts on “Not sure how to include function in useEffect”

Leave a Comment