我最近将一个站点迁移到 Gatsby[v2] 并发现事件发生后 Snipcart 的自定义 html 会成倍增加。如何防止 Snipcart 自定义 HTML 在每次页面导航或事件发生时成倍增加?当我打开模式时,多重渲染是可见的。我不确定这是 React 组件生命周期问题还是 Gatsby[v2] 布局组件问题。
CustomSnipcartText 组件使用 componentDidMount 调用 Snipcart api 并使用这些方法将文本绑定到 DOM。CustomSnipcartText 组件被导入到 Gatsby 布局组件中。我尝试将组件导入到打开模态函数的位置,结果没有变化。
Snipcart 自定义 html 组件:
// Binds the Snipcart subscription services to the component
componentDidMount() {
/* global Snipcart:false */
Snipcart.execute('bind', 'cart.opened', function() {
Snipcart.execute('unbind', 'cart.opened')
/* global $: false */
let html = $('#cart-content-text').html()
$(html).insertBefore($('#snipcart-footer'))
})
} ....
GatsbyJs 布局组件:
export default class Layout extends Component {
render() {
return (
<div className="wrapper">
<CustomSnipcartText /> ...
我希望 CustomSnipcartComponent 在任何事件发生后都不应成倍增加。