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: |
Answered by
igordanchenko
Jun 19, 2023
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 |
1 reply
Answer selected by
belfortf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there!
You can add custom photo attributes by simply adding them to your photo objects, just don't cast the
photosarray toPhoto[].