# imagekit-react-hook

> Hook wrapper for ImageKit.io client-side SDK

Latest version **1.5.4** (published 2022-08-02) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install imagekit-react-hook
pnpm add imagekit-react-hook
yarn add imagekit-react-hook
bun add imagekit-react-hook
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.4 |
| Published | 2022-08-02 |
| First published | 2022-07-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 81.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Bruce Harrison |
| Maintainers | leeharrison1984 |
| Keywords | imagekit, react, hook, cdn |

## Links

- npm: https://www.npmjs.com/package/imagekit-react-hook
- Repository: https://github.com/bruceharrison1984/imagekit-react-hook
- Homepage: https://github.com/bruceharrison1984/imagekit-react-hook#readme
- Issues: https://github.com/bruceharrison1984/imagekit-react-hook/issues
- npm.io page: https://npm.io/package/imagekit-react-hook

## Dependencies (1)

- [imagekit-javascript](https://npm.io/package/imagekit-javascript.md) 1.5.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.5.4 (latest) — 2022-08-02
- 1.5.3 — 2022-07-30
- 1.5.2 — 2022-07-30
- 1.0.3 — 2022-07-30
- 1.0.2 — 2022-07-30
- 1.0.1 — 2022-07-30
- 1.0.0 — 2022-07-30

## README

# imagekit-react-hook

[![npm](https://img.shields.io/npm/v/imagekit-react-hook?color=orange)](https://www.npmjs.com/package/imagekit-react-hook)

This is a simple hook wrapper around the ImageKit.io javascript library. It allows for easy usage of client-side uploads,
as well as generating signed URLs for private images. This is a very direct, non-opinionated wrapper, so the original
ImageKit.io documentation should be referred to for method usage.

## Usage

To use, simply wrap the components you wish to use the `useImageKit` hook with the included `ImageKitProvider`. You must provide the
`urlEndpoint`, `authenticationEndpoint`, and `publicKey` properties.

This example uses NextJS and loads the settings from environment variables, but any framework based on React should be similar:

```tsx
import { ImageKitProvider } from '@/providers/ImageKitProvider';

const PhotosPage = () => {
  const [origin, setOrigin] = useState<string>();

  useEffect(() => setOrigin(window.location.origin), []);

  return (
    <ImageKitProvider
      urlEndpoint={process.env.NEXT_PUBLIC_IMAGEKIT_URL_ENDPOINT!}
      authenticationEndpoint={`${origin}/api/auth/photo`}
      publicKey={process.env.NEXT_PUBLIC_IMAGEKIT_PUBLIC_KEY}
    >
      <child-components />
    </ImageKitProvider>
  );
};

export default PhotosPage;
```

Child components can then access the ImageKitContext via the `useImageKit` hook:

```tsx
const { upload, url } = useImageKit();

const onClick = (file: File) =>
  await upload({
    file,
    fileName: [uuidV4(), ext].join('.'),
    extensions: [
      {
        name: 'google-auto-tagging',
        minConfidence: 90,
        maxTags: 2,
      },
    ],
  });
```

## Upload Method

The `upload` method is for client-side image uploads. This _requires_ the `authenticationEndpoint` property also
being set. It is up to you how you implement this endpoint, but ImageKit.io offers [this documentation](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload#signature-generation-for-client-side-file-upload) on how to
set it up.

## Hook Methods & Props

The following methods and properties are exposed via the `useImageKit` hook:

| Method/Prop            | Type   | Description                                                                |
| ---------------------- | ------ | -------------------------------------------------------------------------- |
| upload                 | method | Used for client-side uploading of images (requires authenticationEndpoint) |
| url                    | method | Generate signed ImageKit.io URLs                                           |
| urlEndpoint            | string | The ImageKit.io endpoint that was passed into the ImageKitProvider         |
| publicKey              | string | The ImageKit.io public key that was passed into the ImageKitProvider       |
| authenticationEndpoint | string | The authentication endpoint that was passed into the ImageKitProvider      |
| imageKitClient         | object | Raw ImageKit SDK client                                                    |

---
_Source: https://npm.io/package/imagekit-react-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
