How to exactly use custom renderer? #115
-
Hi, I'm struggling on how to properly use the custom renderer. I have modified the photos array to include price and category:
By using "alt", I can show the title under the picture (using custom renderer example). However, how do I show the price and category as they're not defined? What I did:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi there! You can add custom photo attributes by simply adding them to your photo objects, just don't cast the const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
alt: photo.title, //I added this line
category: photo.category, // photo category
price: photo.price, // photo price
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
width: breakpoint,
height,
};
}),
})); Then you should be able to access your custom attributes inside the renderPhoto={({
photo: { category, price },
wrapperStyle,
renderDefaultPhoto,
}) => (
<div className="photo" style={wrapperStyle}>
{renderDefaultPhoto({ wrapped: true })}
<span className="category">{category}</span>
<span className="price">${price}</span>
</div>
)} https://codesandbox.io/p/sandbox/react-photo-album-115-7p7dc3?file=%2Fsrc%2FApp.tsx |
Beta Was this translation helpful? Give feedback.
Hi there!
You can add custom photo attributes by simply adding them to your photo objects, just don't cast the
photos
array toPhoto[]
.