1

J'ai un composant qui a besoin de charger des embrayages Pinterest en fonction de l'état de l'application. Les insertions Pinterest consistent en une balise <a> qui est transformée en une mise en page complexe au sein d'une balise <span> grâce à l'utilisation du script async au bas de mon HTML (disponible here). Cependant, j'ai du mal à comprendre comment, après un rendu avec les accessoires mis à jour, effacer le vieux <span> et réexécuter le script pour rendre la nouvelle intégration. Merci!Mise à jour de Pinterest intègre dans l'application Preact

Mon élément:

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; 

Répondre

0

la solution la plus simple est de passer ici à une composante à part entière:

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