Skip to main content

use custom hook for address input - usequery or client.query

useEffect(() => {
const fetchStripeClient = async () => {
const result = await client.query({
query: QUERY_STRIPE_CLIENT_SECRET,
variables: { amount: Math.floor(totalOfOrder * 100) },
})
const {
data: { getStripeClientSecret },
} = result
setStripeClientSecret(getStripeClientSecret)
}
if (fetchingStripeClient) {
fetchStripeClient()
setFetchingStripeClient(false)
}
}, [fetchingStripeClient, client, totalOfOrder])




import React, { useState, useEffect } from 'react'
import { useQuery } from '@apollo/react-hooks'
import { QUERY_LISTING } from './queries'
import { ApolloError } from 'apollo-client'
interface Props {
listingId: string
}

function useListingData<T>(
initialValue: T,
listingId: T
): [T, ApolloError | undefined, React.Dispatch<T>] {
const [listingData, setListingData] = useState<T>(initialValue)

const { loading, error, data } = useQuery(QUERY_LISTING, {
variables: { id: listingId },
})

useEffect(() => {
if (!loading) setListingData(data.getListing)
}, [data, loading])
return [listingData, error, setListingData]
}

export default useListingData

import { useState, useEffect } from 'react';

function useDebounce<T>(
  initialValue: T,
  time: number
): [T, T, React.Dispatch<T>] {
  const [value, setValue] = useState<T>(initialValue);
  const [debouncedValue, setDebouncedValue] = useState<T>(initialValue);

  useEffect(() => {
    const debounce = setTimeout(() => {
      setDebouncedValue(value);
    }, time);
    return () => {
      clearTimeout(debounce);
    };
  }, [value, time]);

  return [debouncedValue, value, setValue];
}


Comments

Popular posts from this blog

About GraphQL - Downside

Web caching complexity

File uploading. Since GraphQL doesn’t understand files, a file uploading feature is not included in its specification. You won’t have to deal with this limitation in case of REST, as there you can POST or PUT whatever content you want to.
To allow file uploads in your GraphQL web app, there are several options: using Base64 encoding. But it will make the request larger and expensive to encode/decode.making a separate API endpoint just for this purpose.using a library like Apollo for implementing the GraphQL multipart request specification.uploadFileToS3:combineResolvers( // isAuthenticated, async (parent, args, { models }) => { const { file } = awaitargs const { createReadStream, filename, mimetype, encoding } = awaitfile conststream = createReadStream() constresult = awaituploadFileToS3(filename, stream) returnresult } ),