Remount componentDidMount() by path.name

前端 未结 4 1582
野性不改
野性不改 2021-01-25 23:11

When the page loads I use componentDidMount() to document.createElement(\"script\"); in the layout index.js of a ReactJS and G

4条回答
  •  灰色年华
    2021-01-25 23:58

    You can use navigateTo from the gatsby-link package to roll your own Link component that executes your custom logic before navigating.

    Link.jsx

    import React from "react";
    import { navigateTo } from "gatsby-link";
    
    const RESOURCE_PATH = "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";
    const refetchAndNavigate = (path) => () => {
      // refetch
      const tripadvisorLeft = document.createElement("script");
      tripadvisorLeft.src = RESOURCE_PATH;
      tripadvisorLeft.async = true;
      document.body.appendChild(tripadvisorLeft);
    
      // finally navigate
      navigateTo(path);
    }
    
    const Link = ({ to, ...propsToPass }) => (
      
    ); export default Link;

    I have not tested this code but idea will work.

提交回复
热议问题