1

enter image description hereI have an defined an array as state variable in a functional component

const [MessageResultsFirebase, setMessagesFirebase] = useState<MessageCellPropsFirebase[]>();

I want to filter it using Array.filter function to update the list.

var filterResult: MessageCellPropsFirebase = MessageResultsFirebase?.filter((element, index, array)=>{
       return (element.key == snapshot.key);
     });

but it gives the error "cannot find property filter of undefined". P.S: console.log(MessageResultsFirebase); works in same context.Please help i am new with these.

shruti
  • 117
  • 1
  • 11
  • @T.J.Crowder the error is being produced even after the optional operator. And Thanks for the tip. But the main question is as soon as i want to filter the the array it throws undefined can you help me with that? – shruti Apr 25 '20 at 14:15

1 Answers1

1

So it was happening due to the type declared in the Usestate function instead of

const [MessageResultsFirebase, setMessagesFirebase] = useState<MessageCellPropsFirebase[]>();

it should be

const [MessageResultsFirebase, setMessagesFirebase] = useState<Array<MessageCellPropsFirebase>>();

Hope it helps someone new Typescript.

shruti
  • 117
  • 1
  • 11