Originally posted by **mnidhi172** March 14, 2024
I am trying to render the mermaid diagrams along with the errors in the mermaid code if any on the webpage using react js. In my app the diagram is reflecting properly however the errors in the code are not going from the page even if the mermaid code is correct.
Here is how I am doing it:
```
import React from 'react';
import mermaid from 'mermaid';
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch';
class Mermaid extends React.Component {
constructor(props) {
super(props);
this.state = {
mermaidError: null,
};
}
componentDidMount() {
mermaid.initialize({
startOnLoad: true
});
mermaid.contentLoaded();
mermaid.parseError = (error, hash) => {
console.error('Mermaid errors:', error);
this.setState({ mermaidError: error.str });
};
}
componentDidUpdate(prevProps) {
if (prevProps.chart !== this.props.chart) {
const mermaidChart = document.getElementById('mermaid-chart');
if (mermaidChart) {
mermaidChart.removeAttribute('data-processed');
mermaid.contentLoaded();
}
}
}
render() {
return (
Discussed in https://github.com/orgs/mermaid-js/discussions/5384
} }
export default Mermaid;
Instead of using
startOnLoad: true
, you can use mermaid's render function to generate the SVG.Example: https://github.com/mermaid-js/mermaid/blob/33287a63ad539959ce842613a5801cd92c5faaef/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue
Docs: https://mermaid.js.org/config/usage.html#api-usage
I have sorted the issue, thanks for the reference. This is how I am doing it: