A very compact representation of an image placeholder for react native
thumbhash
is a A very compact representation of a placeholder for an image. Store it inline with your data and show it while the real image is loading for a smoother loading experience.
To know more about thumbhash click here.
I recommend you to have a pre-encoded thumbhash string and store it in your server. And when your server delivers an image, you can add thumbhash in your payload. You can now immediately show <Thumbhash>
component before your Image
component finishes loading.
yarn add @luckypear/react-native-thumbhash
or
npm install @luckypear/react-native-thumbhash
npx pod-install
you will need pod install
no extra steps are needed
The <Blurhash>
component has the following properties:
Name | Type | Explanation | Default Value |
---|---|---|---|
blurhash |
string |
The thumbhash string to use. Example: 3OcRJYB4d3h/iIeHeEh3eIhw+j2w |
*Required |
decodeAsync |
boolean |
Asynchronously decode the thumbhash on a background Thread instead of the UI-Thread. | false |
resizeMode |
'cover' | 'contain' | 'stretch' |
Sets the resize mode of the image. ('repeat' and 'center' is not supported.)
See: Image::resizeMode |
'cover' |
onLoadStart |
() => void |
A callback to call when the Blurhash started to decode the given blurhash string. |
undefined |
onLoadEnd |
() => void |
A callback to call when the Blurhash successfully decoded the given blurhash string and rendered the image to the <Blurhash> view. |
undefined |
onLoadError |
(message?: string) => void |
A callback to call when the Blurhash failed to load. Use the message parameter to get the error message. |
undefined |
All View props |
ViewProps |
All properties from the React Native View |
undefined |
Example
import { Thumbhash } from '@luckypear/react-native-thumbhash';
// ...
<Thumbhash thumbhash="3OcRJYB4d3h/iIeHeEh3eIhw+j2w" />;
You can encode with this library and generate your thumbhash in your app.
const thumbhash = await Thumbhash.encode('https://picsum.photos/200');
encode method loads image via react-native image loader
, and scales it
and encodes it
in to a thumbhash string. And this means it is a time taking task so you should be aware of using it.
If you want to encode it in your react-native app, I recommend you a tip that you can use thumbhash string to file name that you will set to server (presigned url or cdn or whatever). then you can just make your own Image
component to extract the file name(which is a thumbhash string) and display it before you load your image. this will save some bytes for your playload :)
- thumbhash rendering (decoding)
- new arch/old arch support
- async dencoding
- thumbhash encoding
-
resizeMode
support
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library
- react-native-blurhash. referenced many setting. thx!
- woltapp/blurhash. Thanks for great algorithm