SanityImage in your app. This centralizes configuration and provides an entry point for custom logic.
Why Create a Wrapper?
Without a Wrapper
You’ll repeat thebaseUrl (or projectId and dataset) everywhere:
With a Wrapper
Configuration is set once, usage is cleaner:Basic Wrapper Implementation
Here’s the simplest wrapper component:How It Works
- Generic Type Parameter -
<T extends React.ElementType = "img">enables polymorphic rendering - WrapperProps Type - Omits configuration props (like
baseUrl) from the component’s API - Spread Props -
{...props}passes through all other props toSanityImage - Centralized Config -
baseUrlis set once in the wrapper
Using the Wrapper
Once created, use yourImage component throughout your app:
TypeScript Support
The wrapper maintains full TypeScript support and polymorphism:as prop!
Complete Example with Custom Logic
Here’s a more advanced wrapper with additional features:- Uses environment variables for project configuration
- Sets a default image quality
- Warns in development when
alttext is missing - Allows all defaults to be overridden
Configuration Options
Using baseUrl
If you have the full URL:
Using projectId and dataset
If you prefer separate configuration:
Advanced: Multiple Environments
Handle different environments in your wrapper:Emotion jsxImportSource Workaround
If you’re using Emotion’s jsxImportSource, TypeScript may complain about polymorphic props. Use this workaround:
<Image>.
The WrapperProps Type
The WrapperProps<T> type is exported specifically for creating wrappers:
Omitting Additional Props
You can omit additional props from your wrapper’s API:Where to Place the Wrapper
Common locations:Testing Your Wrapper
Test that your wrapper passes props correctly:Next Steps
GROQ Queries
Learn how to fetch image data from Sanity
API Reference
See all available props and options