First all my question is not rehash of ES6 double destructure
Look at the following code - Apollo Client GraphQL
import { gql, useQuery, useMutation } from '@apollo/client';
...
const { loading, error, data } = useQuery(TREATMENTS);
It would be nicer to write it this way:
const { loading, error, data : {treatments} } = useQuery(TREATMENTS);
However, unfortunately I've got the following error:
TypeError: Cannot read property 'treatments' of undefined
TreadDetails
C:/Users/albertly/Downloads/git/individual-claims/src/TreatDetails.tsx:35
32 | `;
33 |
34 | function TreadDetails(): React.ReactElement {
> 35 | const { loading, error, data : {treatments} } = useQuery(TREATMENTS);
36 | // const [treatments, setTreatments] = useState<Treatment[]>([]);
37 | const { state: stateApp, dispatch: dispatchApp } = useContext(AppContext);
I understand perfectly well why it is happening.
My question is: Is there some syntactical trick to make it work ?
Edited: It has been brought to my attention that there is a quite similar question Destructuring with nested objects and default values