javascript - 更新 Preact 应用程序中的 Pinterest 嵌入

标签 javascript reactjs single-page-application pinterest preact

我有一个组件需要根据应用程序的状态加载不断变化的 Pinterest 嵌入内容。 Pinterest 嵌入由 <a> 组成标签被转换为 <span> 中的复杂布局通过使用 HTML 底部的异步脚本进行标记(可用 here )。但是,我无法弄清楚如何在使用更新的 Prop 重新渲染时清除旧的 <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;

最佳答案

这里最简单的解决方案是切换到完整组件:

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>
    );
  }
}

关于javascript - 更新 Preact 应用程序中的 Pinterest 嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45511418/

相关文章:

javascript - ReactJS - Prop 命名的首选约定

angularjs - Asp.net core中的SPA预渲染?

javascript - 检查 Javascript 中的日期和时间比较

javascript - Bootstrap 选项卡不适用于不同的类名

javascript - 将 fetchPolicy 设置为 "standby"时无法调用 client.watchQuery - 从 useLazyQuery 加载页面时引发错误

javascript - 从其他组件中的服务访问数据

javascript - AngularJS 如何将多个 JS 文件组合成一个文件?

javascript - 在登录页面和仪表板之间 Split View的最佳方式 Routing Vue js for SPA

javascript - CouchDB 验证可防止 DELETE

javascript - Angularjs 到 angular4 到 angularjs