1

我有一个组件需要根据应用程序的状态加载更改Pinterest嵌入。 Pinterest嵌入由一个<a>标签组成,通过使用我的HTML底部的异步脚本(可用here)将其转换为<span>标签内的复杂布局。但是,我很难弄清楚如何通过使用更新后的道具进行重新渲染来清除嵌入的旧<span>并重新运行脚本以呈现新的嵌入。谢谢!正在更新Pinterest嵌入Preact App

我的组件:

const PinterestEmbed = ({ location, size }) => (
    <div stye="text-align: center;margin: auto;"> 
     <a 
      data-pin-do="embedPin" 
      data-pin-width={size} 
      href={location}> 
      Pin via Pinterest 
     </a> 
    </div> 
); 

export default PinterestEmbed; 

回答

0

这里最简单的解决办法是切换到全成分:

export default class PinterestEmbed extends Component { 
    // keep a reference to the link so we can update it: 
    linkRef = el => { 
    this.link = el; 
    }; 
    // never re-render using virtual dom: 
    shouldComponentUpdate() { 
    return false; 
    } 
    // instead, we'll handle location changes ourselves: 
    componentWillReceiveProps(nextProps) { 
    if (nextProps.location!==this.props.location) { 
     this.link.href = nextProps.location; 
     // do something to tell Pinterest to update (not sure how they expose it): 
     Pinterest.reInitialize(this.link); 
    } 
    } 
    render({ location, size }) { 
    return (
     <div stye="text-align: center;margin: auto;"> 
     <a 
      ref={this.linkRef} 
      data-pin-do="embedPin" 
      data-pin-width={size} 
      href={location}> 
      Pin via Pinterest 
     </a> 
     </div> 
    ); 
    } 
}