Array from Cache Not Extensible?

I’ve got a React useRef that I use to store my appointments when they come in from a useQuery:

    Appts.current = ((data) && (data.Appts) ? data.Appts : []);

I also have a useSubscription that adds the incoming appointment to the useRef and sorts the list:

  Appts.current.push(newAPPT);
  Appts.current = Appts.current.sort(sortApptsByDateHelper);

This has worked fine for years. :slight_smile: I updated to the latest libraries, and now Appts.current.push(newAPPT) is throwing this error:

Uncaught TypeError: Cannot add property 8, object is not extensible
at Array.push ()

I’m looking at the docs for useSubscription, and I don’t yet see how to handle this.

What are the best practices now for this kind of situation?

Hi, have you tried doing the following instead?

Appts.current = [...Appts.current, newAPPT];

Data returned from apollo is immutable, and in recent versions that immutability is being enforced.

2 Likes

That worked great. Thanks!