I create a list of Clients, where the user clicks one of the Clients they will Link to the page and send the data as a state. I get the data from portfolio that I fetched before using Context
{portfolio.map((data) => {
const { id, name, image, logo, year, title, text, path } = data;
return (
<Link to={`/portfolio/${path}`} key={id} state={data}>
<div
className={`grid-item grid-item--${id}`}
style={{
backgroundImage: `url(${image})`,
backgroundSize: "cover",
backgroundPosition: "center center",
}}
>
<img src={logo} alt={name} className="cls-logo" />
<div className="cls-text">
<div className="cls-text-wrapper">
<h1>{year}</h1>
<h3>{title}</h3>
<p>{text}</p>
</div>
</div>
</div>
</Link>
);
})}
It succeeds for a moment, then I added a Link to make it easier to navigate by adding the prev and next Client if they want to see it.
let data = useLocation();
const { id, name, logo, image, text, title, year } = data.state;
const { portfolio } = useGlobalContext();
let prev = id - 1;
if (prev === 0 && portfolio !== []) {
prev = portfolio.length - 1;
}
let next = id + 1;
if (next > portfolio.length - 1 && portfolio !== []) {
next = 1;
}
The problems that I'm getting are:
Seems that I cant use
portfoliobecause it's undefined at the first, soReactcan't catch the values from it.If I comment the
prev&nextmy code works just fine, and when I hit the refresh button it's also fine. But the problem is when I try to access it directly from the URL since I didn't send any data to the component.
Appreciate any kinda response, thanks before.