1

我有一个组件需要根据应用程序的状态加载更改的 Pinterest 嵌入。Pinterest 嵌入由一个标签组成,该标签通过使用我的 HTML 底部的异步脚本(可在此处<a>获得)转换为标签内的复杂布局。但是,我无法弄清楚如何在使用更新的道具重新渲染时清除旧嵌入并重新运行脚本以渲染新嵌入。谢谢!<span><span>

我的组件:

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;
4

1 回答 1

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>
    );
  }
}
于 2017-08-06T07:31:15.290 回答