When the page loads I use componentDidMount() to document.createElement("script");
in the layout index.js
of a ReactJS and GatsbyJS project as
componentDidMount () {
const tripadvisorLeft = document.createElement("script");
tripadvisorLeft.src = "https://www.jscache.com/wejs?wtype=selfserveprop&uniq=789&locationId=10467767&lang=en_NZ&rating=true&nreviews=0&writereviewlink=true&popIdx=true&iswide=true&border=false&display_version=2";
tripadvisorLeft.async = true;
document.body.appendChild(tripadvisorLeft);
}
This then requests the data to be displayed and works fine. However, when I <link to=...
another page using gatsby-link
(imagine the same issue applies for react-router
), componentDidMount()
has already run so it won't fetch the data again.
How can I ensure that this script is mounted
after each path
change, or better by a specific path
?