I understand I have an issue similar to here, Though It looks to me that this is what I am already doing.
I have a function which ads the script to the page as per below.
export const appendScript = function (scriptToAppend, sourceScript = true) {
if (sourceScript) {
const script = document.createElement("script");
script.type = "text/javascript";
script.async = false;
script.src = scriptToAppend;
document.body.appendChild(script);
} else {
const script = document.createElement("script");
script.type = "text/javascript";
script.async = false;
script.innerHTML = scriptToAppend;
document.body.appendChild(script);
}
};
export const removeScript = (scriptToremove) => {
let allsuspects = document.getElementsByTagName("script");
for (let i = allsuspects.length; i >= 0; i--) {
if (
allsuspects[i] &&
allsuspects[i].getAttribute("src") !== null &&
allsuspects[i].getAttribute("src").indexOf(`${scriptToremove}`) !== -1
) {
allsuspects[i].parentNode.removeChild(allsuspects[i]);
}
}
};
I add the scripts to the HTML as:
componentDidMount() {
//Jquery Plugin from the asset site. These are used always.
appendScript("assets/js/jquery-2.2.1.min.js");
appendScript("assets/js/jquery-migrate-1.2.1.min.js");
appendScript("assets/bootstrap/js/bootstrap.min.js");
}
Though one of the scripts is not loading properly when navigating through the navigation (via <Link></Link>
component.
Though once I refresh the page, the script is working and I can see the script being used, e.g. pictures are showing up, owl carousel, and other visible effects.
The pagination, I never get the script to work on the next page, mainly the img are not showing up.
{pages.map((page) => (
<li key={page} className={page === currentPage ? "active" : "null"}>
<Link to="#" onClick={() => onPageChange(page)}>
{" "}
{page}
</Link>
</li>
))}
Maybe this is as well something in the way how I am retrieving img elements in React:
<div className="image bg-transfer">
<img src={tag.imagePath} alt="Sorry Can't Find :(" />
</div>
Is this approach correct in order to have the scripts loaded for the react? I can't have the scripts changed much, moving from a static HTML draft to React.js. I hope there is a simple answer to this...