Masir
Masir

Reputation: 337

filter function is doesn't work on Array in React

I get data from the server by using useReducer and do some action on that and showing them on the table for that I use filter() but it gives me an error that my data is Undefined

UseReducer:

function savedEventsReducer(state, { type, payload }) {
  switch (type) {
    case "push":
      return [...state, payload];
    case "update":
      return state.map((evt) =>
        evt.id === payload.id ? payload : evt
      );
    case "delete":
      return state.filter((evt) => evt.id !== payload.id);
    default:
      throw new Error();
  }
}
 const [SavedEvents, dispatchcallEvent] =
    useReducer(savedEventsReducer, [])
  useEffect(() => {
      axios.get('http://localhost:8000/SavedEvents/').then(resp => {
        dispatchcallEvent({ type: 'push', payload: resp.data });
      })
  }, [])


These are the action functions that filter data:

  const [Lables, SetLables] = useState([])

const filteredEvents = useMemo(() => {
    if(SavedEvents[0]){ 
      console.log(SavedEvents[0]); // it's gives me my Data and not Undefine.
      console.log(SavedEvents);
      return SavedEvents[0].filter((evt) => // this is the line that mentioned in Error 
      Lables
        .filter((lbl) => lbl.checked)
        .map((lbl) => lbl.label)
        .includes(evt.label)
    );}
  }, [SavedEvents, Lables])
useEffect(() => {
    SetLables((prevLabels) => {
      if(SavedEvents[0]){
      return [...new Set(SavedEvents[0].map((evt) => evt.label))].map(
        (label) => {
          const currentLabel = prevLabels.find(
            (lbl) => lbl.label === label
          );
          return {
            label,
            checked: currentLabel ? currentLabel.checked : true,
          };
        }
      );
      }
    });
  }, [SavedEvents])

All these codes are in my Context and I use them after the first rendering all of them are rendered

Error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'filter')
    at ContextWrapper.js:58:1
    at Array.filter (<anonymous>)
    at ContextWrapper.js:58:1
    at updateMemo (react-dom.development.js:15867:1)
    at Object.useMemo (react-dom.development.js:16413:1)
    at useMemo (react.development.js:1532:1)
    at ContextWrapper (ContextWrapper.js:54:1)
   

this is SavedEvents[0] :

enter image description here

Upvotes: 0

Views: 346

Answers (1)

Mohsen.RJ
Mohsen.RJ

Reputation: 384

you should add another condition on top of your SetLables Like :

useEffect(() => {
    if(SavedEvents[0]){
    SetLables((prevLabels) => {
        // console.log(SavedEvents[0]);
      return [...new Set(SavedEvents[0].map((evt) => evt.label))].map(
        (label) => {
          const currentLabel = prevLabels.find(
            (lbl) => lbl.label === label
          );
          return {
            label,
            checked: currentLabel ? currentLabel.checked : true,
          };
        }
      );
    });
  }
    // console.log(Lables);
  }, [SavedEvents])

Upvotes: 1

Related Questions