0.8.12 • Published 1 year ago

@rest-hooks/img v0.8.12

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

\ - Suspenseful Image

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome

Suspenseful image component: <Img />.

📖Read The Docs

In many cases, it would be useful to suspend loading of expensive items like images using suspense. This becomes especially powerful with the fetch as you render pattern in concurrent mode.

Here, we build an endpoint for images using Image

Here, Rest Hooks is simply used to track resource loading - only storing the src in its store.

Usage

import React, { ImgHTMLAttributes } from 'react';
import { useResource } from 'rest-hooks';
import { Img } from '@rest-hooks/img';

export default function Profile({ username }: { username: string }) {
  const user = useResource(UseResource.detail(), { username });
  return (
    <div>
      <Img
        src={user.img}
        alt="React Logo"
        style={{ height: '32px', width: '32px' }}
      />
      <h2>{user.fullName}</h2>
    </div>
  );
}

Prefetching

Note this will cascade the requests, waiting for user to resolve before the image request can start. If the image url is deterministic based on the same parameters, we can start that request at the same time as the user request:

import React, { ImgHTMLAttributes } from 'react';
import { useResource, useRetrieve } from 'rest-hooks';
import { Img, getImage } from '@rest-hooks/img';

export default function Profile({ username }: { username: string }) {
  const imageSrc = `/profile_images/${username}}`;
  useRetrieve(getImage, { src: imageSrc });
  const user = useResource(UseResource.detail(), { username });
  return (
    <div>
      <Img
        src={imageSrc}
        alt="React Logo"
        style={{ height: '32px', width: '32px' }}
      />
      <h2>{user.fullName}</h2>
    </div>
  );
}

When using the fetch as you render pattern in concurrent mode, useFetcher with the getImage Endpoint to preload the image.

0.8.9

1 year ago

0.8.8

1 year ago

0.8.12

1 year ago

0.8.11

1 year ago

1.0.0-next.0

1 year ago

0.8.10

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.7

1 year ago

0.8.6

1 year ago

0.8.1

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.6.7

2 years ago

0.6.6

2 years ago

0.6.8

2 years ago

0.7.2

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.5-beta.2

2 years ago

0.6.5-beta.1

2 years ago

0.6.5-beta.0

2 years ago

0.6.3

2 years ago

0.6.5

2 years ago

0.6.4

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.5.0-beta.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.2-beta.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago