HOME/JOURNAL/USING MINDBODY BRANDED WEB WIDGETS WITH REACT
OCM LABS

Using MindBody Branded Web Widgets with React

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.

Shane Blandford
Shane Blandford · FOUNDERPUBLISHED 2020.12.16 · UPDATED 2025.02.11 · 4 MIN READ

MindBody Branded Web Widgets with React.

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.

Step 1. Create React ErrorBoundary Component

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;

Step 2. Create MindBody Component

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;

Step 4. Create Component to Display Widget

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;

Step 4. Wrap MindBody Component in React Router

function App() {
return (
<Router>
<Switch>

<Route
path="/mindbody-page"
exact
render={() => (
<ErrorBoundary>
<MindBodyPage />
</ErrorBoundary>
)}
/>

<Route path="/" component={HomePage} />
<Route component={HomePage} />
</Switch>
</Router>
);
}

Shane Blandford

Shane Blandford

FOUNDER

Founder of Orange Collar Media, a Denver ecommerce agency behind 800+ Magento and Shopify builds. In Magento since version 1.x - building, rescuing, and supporting revenue-critical stores since 2008.

All posts by Shane Blandford

Your store can sell more. Let's find out how much.

Start a project