Skip to content

Instantly share code, notes, and snippets.

export const useAPI = (query) => {
const data = useMemo(() => {
return wrapGraphQL(query);
}, [query]);
return { data };
};
export const wrapGraphQL = (query) => {
let status = "pending";
let result = null;
const suspender = client
.query({
query,
fetchPolicy: "network-only"
})
.then((response) => {
status = "success";
import { gql } from "@apollo/client";
export const GET_LAUNCHES = gql`
query GetLaunches {
launchesPast(limit: 10) {
mission_name
launch_date_local
launch_site {
site_name_long
}
import { ApolloClient, InMemoryCache } from "@apollo/client";
export const client = new ApolloClient({
uri: "https://api.spacex.land/graphql",
cache: new InMemoryCache()
});
const initialResource = fetchProfileData();
function ProfilePage() {
return (
<Suspense fallback={<h1>Loading profile...</h1>}>
<ProfileDetails resource={initialResource} />
</Suspense>
);
}
{
"op": "replace",
"path": ["profile"],
"value": {"name": "Veria", "age": 5}
}
import produce from "immer"
const baseState = [
{
todo: "Learn typescript",
done: true
},
{
todo: "Try immer",
done: false
import produce, { applyPatches, enablePatches } from "immer";
enablePatches();
export const RESET = "RESET";
export const OPEN_DOOR = "OPEN_DOOR";
export const UNDO = "UNDO";
let inverseChanges = [];
export const reducer = (state, action) => {
import produce, { enablePatches } from "immer";
// some previous code is ommited for brevity
enablePatches();
let inverseChanges = [];
export const reducer = (state, action) => {
return produce(
const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
const { doors } = state;