I am trying to load ecwid store on my react.js website.
I have created a class component where there is a function called load ecwid.
I have put the function in the componentDidMount()
.
The console logs tell me the function is loaded well and good. However, nothing shows up on the screen unless I manually refresh the page.
The following is my code:
class EcwidStore extends React.Component {
state = {
isLoaded: false,
};
loadEcwid = () => {
var isEcwidPage = document.getElementById('productBrowser') != null;
if (window.ecwidLoaded && isEcwidPage) return;
window.ecwidLoaded = true;
window.ecwid_script_defer = true;
window.ecwid_dynamic_widgets = true;
window.ec.storefront = window.ec.storefront || Object();
window.ec.enable_catalog_on_one_page = true;
window._xnext_initialization_scripts = [
{
widgetType: 'ProductBrowser',
id: 'my-store-xxx',
arg: ['id=productBrowser', 'views=grid(20,3)'],
},
{
widgetType: 'CategoriesV2',
id: 'my-categories-xxx',
arg: ['id=categoriesV2'],
},
{
widgetType: 'SearchWidget',
id: 'my-search-xxx',
arg: ['id=searchWidget'],
},
];
if (typeof Ecwid != 'undefined') {
} else {
var script = document.createElement('script');
script.charset = 'utf-8';
script.type = 'text/javascript';
script.id = 'ecwid-script';
script.src = 'https://app.ecwid.com/script.js?xxx&data_platform=code&data_date=2021-01-14';
document.getElementById('my-store-xxx').appendChild(script);
}
};
componentDidMount() {
this.loadEcwid();
this.setState({ isLoaded: true });
console.log('loaded!');
this.render();
}
render() {
return (
<div>
<div id="my-search-xxx" />
<div id="my-store-xxx" />
<div id="productBrowser" />
<div id="my-categories-xxx" />
<div className="ec-cart-widget" />
</div>
);
}
}
export default withRouter(EcwidStore);