I have been trying to render image in an external web-component inside react but couldn't do it.
I am working on a react app that would use HTML template that can be run in browser, template is something like:
<script src="https://assets.some-domain.com/someBundledjs.js"></script>
<ParentForm>
<ChildForm name="entryForm" image-src="https://www.image-domain.com/person1.jpg">
</ChildForm>
</ParentFrom>
I tried to render it in reactDOM using react-helmet. Everything works fine except the image doesn't load in browser(though the call to fetch the image succeeds). When I debugged(luckily I have access to source code generating above form), I came to know that that the image is loading during server-side rendering, before js is downloaded, hence the image.onLoad() callback in the source code is never called. I verified it as per ref.
Update(rendering js without react-helmet):
I tried rendering below in react after loading script by doing something like:
componentDidMount () {
const script = document.createElement("script");
script.onload = () => {
// script has loaded, you can now use it safely
this.setState({ isLoadParentForm: true });
}
script.src = "https://assets.some-domain.com/someBundledjs.js";
script.async = false;
document.body.appendChild(script);
}
render(){
return(
<div>
{this.state.isLoadParentForm?
<ParentForm>
<ChildForm name="entryForm" image-src="https://www.image-domain.com/person1.jpg">
</ChildForm>
</ParentFrom>
: <></>
</div>
)
}
This ensures that the component is rendering post the script has loaded, still the image doesn't load inside the component.
Can someone explain and provide solution?