Remount componentDidMount() by path.name

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

    Could you use a HOC to accomplish what you want. I just threw this together, it will need to be modified.

    Create a component something like,

    const withTracking = (WrappedComponent) => {
        return class withTracking extends React.Component {
            constructor(props) {
                super(props);
    
                this.trackit = this.trackIt.bind(this);
            }
    
            trackIt() {
                {
                    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);
                }
            }
    
            render() {
                return (
                    
    ); } } };

    Then in react router wrap it like,

    
    

    And in the component use componentDidMount,

    const TEST = (WrappedComponent) => {
        return class ClickLogger extends React.Component {
            constructor(props) {
                super(props);
            }
    
            componentDidMount() {
                this.trackIt();
            }
    
            render() {
                return (
                    

    something

    ); } } };

提交回复
热议问题