weikee94 / design-patterns

Design Patterns
0 stars 0 forks source link

Hook Pattern #14

Open weikee94 opened 11 months ago

weikee94 commented 11 months ago

Benefits

Screenshot 2023-09-25 at 5 49 18 PM Listings.js ```js import React from 'react'; import { Listing } from './Listing'; import { ListingsGrid } from './ListingsGrid'; import useListings from '../hooks/useListings'; export default function Listings() { const listings = useListings(); if (!listings) return null; return ( {listings.map((listing) => ( ))} ); } ``` useListings.js ```js import React from 'react'; export default function useListings() { const [listings, setListings] = React.useState(null); React.useEffect(() => { fetch('https://house-lydiahallie.vercel.app/api/listings') .then((res) => res.json()) .then((res) => setListings(res.listings)); }, []); return listings; } ``` [stackblitz](https://stackblitz.com/edit/react-ts-xrwfcv?file=App.tsx)