Remount componentDidMount() by path.name

前端 未结 4 1482
野性不改
野性不改 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-26 00:11

    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.

提交回复
热议问题