Hopefully, this will help someone else out. We sunk more time than we should have to integrate MindBody Branded Web Widget into a client’s React site.
The issue that you will experience is on the first-page load the widgets render correctly. Any further page loads will result in errors that are visible in the console.
Without going too far into the technical weeds the issue is that the healcode javascript loads additional scripts on the page. This causes issues when using react-router to displays different components based on the URL.
The solution is hacky, but until MindBody updates this code there are not many great options to solve this.
The solution in short is to use react error boundaries to catch these errors and perform a browser reload.
import React, { Component } from 'react'
class ErrorBoundary extends Component {
constructor(props){
super(props)
this.state = { error: null, errorInfo: null }
}
componentDidCatch(error, info){
this.setState({ error: error, errorInfo: info })
window.location.reload();
}
render() {
if(this.state.errorInfo){
return (
<div className="error-boundary">
<summary>Something went wrong</summary>
<details style={{ whiteSpace: 'pre-wrap' }}>
{ this.state.error && this.state.error.toString() }
{ this.state.errorInfo.componentStack }
</details>
</div>
)
}
return this.props.children
}
}
export default ErrorBoundary;
const loadMBody = (callback) => {
const existingScript = document.getElementById('mindBody');
if (!existingScript) {
const script = document.createElement('script');
script.src = 'https://widgets.mindbodyonline.com/javascripts/healcode.js';
script.id = 'mindBody';
script.async = true;
script.crossorigin = 'anonymous';
document.body.appendChild(script);
script.onload = () => {
if (callback) callback();
};
}
if (existingScript && callback) callback();
};
export default loadMBody;
import { Container } from '@material-ui/core';
import React, { useEffect, useState } from 'react';
import loadMBody from "./static/scripts/mindbody";
const MindBodyPage = (props) => {
const [mindBody, setMindBody] = useState(false);
useEffect(() => {
loadMBody(() => {
setMindBody(true);
});
}, []);
return (
<div>
<Container>
{mindBody && (
<healcode-widget
data-type="class_lists"
data-widget-partner="object"
data-widget-id="XXXXXXXXXX"
data-widget-version="0">
</healcode-widget>
)}
</Container>
</div>
);
};
MindBodyPage.propTypes = {};
export default MindBodyPage;
function App() {
return (
<Router>
<Switch>
<Route
path="/mindbody-page"
exact
render={() => (
<ErrorBoundary>
<MindBodyPage />
</ErrorBoundary>
)}
/>
<Route path="/" component={HomePage} />
<Route component={HomePage} />
</Switch>
</Router>
);
}
Using MindBody Branded Web Widgets with React
7 key elements for Shopify store growth in 2020
10 Google shopping ads Strategies to up Your Game in 2020
HOW TO BUILD A KILLER PRODUCT COLLECTION ON SHOPIFY
HOW TO BE A SHOPIFY SHIPPING EXPERT
HOW TO CREATE PRODUCT IMAGES THAT REALLY CONVERT
HOW TO SET UP AN ONLINE SHOP ON THE FLY
HOW TO UX-OPTIMIZE YOUR SITE
HOW TO GET QUALITY CUSTOMER REVIEWS
POWERFUL PRODUCT PAGES THAT REALLY SELL
HOW TO CREATE A HYPERLOCAL MARKETING CAMPAIGN
HOW TO SEO OPTIMIZE YOUR ECOMMERCE STORE
HOW TO USE GOOGLE SMART SHOPPING
7 WAYS TO SPEED UP A MAGENTO SITE
HOW TO KEEP CUSTOMERS LOYAL
1630 Welton St. #940Denver, CO 80202
1630 Welton St. #940Denver, CO 80202