I have an array of projects. Each project contains multiple images. By iterating through this array, I display each name. I assigned Link
to each item navigation to a path containing the project's name. My intention was to get this name and then display the clicked project's images.
The project page does also contain a next project button - clicking this button will change the path. That's how I get the new project:
cart;
nextCart;
componentDidUpdate(prevProps, prevState, snapshot) {
const prevPathname = prevProps.location.pathname;
const pathname = this.props.location.pathname;
if (pathname !== prevPathname) {
this.getCarts();
}
}
getCarts = () => {
const name = this.props.match.params.name;
this.cart = this.props.carts.find(value => value.name === name);
const currentIndex = this.props.carts.findIndex(value => value.name === name);
const nextIndex = currentIndex < this.props.carts.length - 1 ? currentIndex + 1 : 0;
this.nextCart = this.props.carts[nextIndex];
};
The problem is, that even though the images change, it happens after a short delay after changing the path. Is there a better way to achieve this behavior?
In other words, when I click the button to change the path, there's a delay until the old project's images disappear and will be replaced with the new.
How I change the path:
this.props.history.push('/project/' + this.nextCart.name);
App.js
function App() {
...
return (
<div className="App">
<Router history={history}>
<ScrollToTop>
<Header/>
{projects ?
<div>
<Route path="/project/:name" component={() => <Project carts={projects}/>}/>
<Route path="/about" component={About}/>
<div className="hs">
<Route path={["/", "/project/:name"]} exact
component={() => <CartHolder carts={projects}/>}/>
</div>
<div className="hs-mobile">
<Route path="/" exact component={() => <CartHolderMobile carts={projects}/>}/>
</div>
</div>
: <div className="black-overlay"/>}
</ScrollToTop>
</Router>
</div>
)
}