When the page loads I use componentDidMount() to document.createElement(\"script\");
in the layout index.js
of a ReactJS and G
You can add in the componentDidUpdate
lifecycle method which is called whenever the component is updated:
componentDidUpdate(prevProps) {
// check if path has changed
if (prevProps.pathname !== this.props.pathname) {
// call something to fetch data
}
}
You can add more conditions to match only certain paths.
Check your gastby-link
library to see how you can get the current pathname to pass in as props.
You can also try passing pathname={window.location.pathname}
from a parent component that gets re-rendered when path changes.