Highlight of my problem. I'm trying to add conditional rendering to the image prop. Some "Stories" I pull from the API don't have images so I need to account for that so I don't get the "TypeError: Cannot read property '0' of null" error, but this still isn't working when I add the ternary operator.
<Story
key={idx}
title={story.title}
abstract={story.abstract}
img={story.multimedia[0].url ? story.multimedia[0].url : null} // why doesn't this work?
alt={story.multimedia[0].caption ? story.multimedia[0].caption : null} // why doesn't this work?
link={story.url}
/>;
Can I add a fallback link to a "placeholder" image URL?
img={story.multimedia[0].url ? story.multimedia[0].url : "https://www.example.com/example.png"}
Full component here
export default function News() {
const [error, setError] = useState(null);
const [stories, setStory] = useState(null);
useEffect(() => {
const getCurrentPage = () => {
const url = new URL(window.location.href);
const page = url.pathname.split("/").pop();
return page ? page : "home";
};
const section = getCurrentPage();
fetch(
`https://api.nytimes.com/svc/topstories/v2/${section}.json?api-key=4fzCTy6buRI5xtOkZzqo4FfEkzUVAJdr`
)
.then((res) => res.json())
.then((data) => {
setTimeout(() => setStory(data), 1500);
console.log("Success ", data);
})
.catch((error) => {
console.log("Error", error);
setError(error);
});
}, []);
if (error) {
return <div>Error: {error.message}</div>;
} else if (!stories) {
return <LoadingBar type={"cylon"} color={"#193152"} />;
} else {
return (
<>
<ul className="stories">
{stories.results.map((story, idx) => {
return (
<Story
key={idx}
title={story.title}
abstract={story.abstract}
img={story.multimedia[0].url ? story.multimedia[0].url : null} // why doesn't this work
alt={ story.multimedia[0].caption ? story.multimedia[0].caption : null } // why doesn't this work?
link={story.url}
/>
);
})}
</ul>
<Spacer height={100} />
</>
);
}
}