This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function useCreateThing() { | |
const dispatch = Redux.useDispatch() | |
return React.useCallback(async (thing) => { | |
dispatch({ type: 'createThing' }) | |
const response = await fetch('/api/thing/create', { body: thing }) | |
if (response.ok) { | |
dispatch({ type: 'createThingSuccess', response }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useConstantCallback(fn) { | |
// store a copy of the function | |
const copyOfFunction = React.useRef(fn); | |
// create a function that always has the same reference (won't trigger a useEffect) | |
const alwaysTheSameFunction = React.useCallback( | |
(...args) => copyOfFunction.current(...args), | |
[], | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Hook = ({ useHook, with }) => { | |
useHook(with); | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useFileReadAsDataURL(file) { | |
const [dataURL, setDataURL] = React.useState(""); | |
const fileReaderRef = React.useRef(new FileReader()); | |
React.useEffect(() => { | |
const fileReader = fileReaderRef.current; | |
const setResult = () => { | |
setDataURL(fileReader.result); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { graphql } from "graphql"; | |
import { makeMockedSchema } from "../../../src/graphQLMock"; | |
const resolve = result => ({ | |
json: () => Promise.resolve(result), | |
text: () => Promise.resolve(JSON.stringify(result)), | |
ok: true, | |
}); | |
const fetchMockFor = schema => (_, { body }) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { ApolloProvider } from "@apollo/react-hooks"; | |
import { ApolloClient } from "apollo-client"; | |
import { InMemoryCache } from "apollo-cache-inmemory"; | |
import { ApolloLink, Observable } from "apollo-link"; | |
export const createErrorLink = ({ errors = null }) => { | |
if (errors) { | |
return new ApolloLink( | |
() => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useSetTimeout(callback, delay) { | |
const savedCallback = useRef() | |
const savedTimeout = useRef(() => {}) | |
function runTimeout() { | |
savedTimeout.current() | |
} | |
useEffect(() => { | |
savedCallback.current = callback; | |
}, [callback]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Following https://graphql.org/learn/pagination/ pagination spec | |
*/ | |
import { useQuery } from '@apollo/react-hooks' | |
function usePaginationFor(name, queryResult) { | |
const { data = {}, fetchMore, variables, ...rest } = queryResult | |
const { pageInfo = {} } = data[name] || {} | |
const fetchMoreAndMerge = () => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react"; | |
import { useLazyQuery } from "@apollo/react-hooks"; | |
function useDeferredQuery(query) { | |
const [load] = useLazyQuery(query); | |
useEffect(load, []); | |
} |