Following on from my article about the use-cloudinary useUpload hook - use-cloudinary - useUpload hook, this time we'll be taking a look at the useSearch hook, and its a powerful beast!
My use case is simple, Users upload their profile and cover image to cloudinary via the useUpload hook in devpack. Each image is saved to a corresponding folder with their name and a suffix of either cover or profile. When the user next logs in to devpack they can choose to upload one of their previous 3 images that were saved in their cloudinary folders, or just upload a new image and use that one.
This is where useSearch comes into play. I use it by calling a serverless function which I pass a search function with certain search parameters. Lets look at some code to get an idea of what Im talking about.
This function takes an event:
- Parses the body passing in the expression which defines our search parameters which we will define in our search function below. (Note that at the time of writing this is nested)
- Sets the max results to 3
- Returns an a okey 200 and the result of the search
I've removed styles from this component
Lets break this down.
- The useSearch hook takes an endpoint, in our case our serverless function. It returns a search function, the data and some states.
- The context holds our users name.
- The customConfigSearch is the power house of this functionality. You can pass it any number of things to search by, in our case its the folder name. We use the = symbol to indicate that we want an exact match. Its wrapped in a useCallback to prevent re-rendering based on the prop changes and stop our serverless function being called again and again. This saves us on our rate limit with cloudinary.
- The expression itself is written using the string literal syntax stating the type of media we are searching for and the exact match on the folder name.
- The config object is optional and can also include any number of options such as tags, sort_by, max_results and many more. Note that some of these can also be added at the serverless function level. Check the docs for more info on whats possible
- The states are handled with custom error and loading components- If the folder is a match and the data was retrieved successfully then the data array is accessed
- The image is returned as a lazy image, which we will cover next!
- A button calls the customConfigSearch function to load the images.
Phew! That was a lot to take in! 😅
The lazy image is a component that lazy loads an image with a placeholder. Handy!
Lazy load image
One thing to note about using this component with Gatsby is that the base image url wont load the image, you have to use the https secure_url for it to work.
And thats it! It's a pretty powerful way to load your images via user defined events. Check the useSearch docs for a run down of whats possible with the hook.