I'm having trouble inserting a third-party script inside a functional component with server-side rendering.
This is the script I should include in a normal HTML document.
<script class="feedaty_sdk" src="https://widget.feedaty.com/public/js/feedaty.js?merchant=******&style_ver=2021" async ></script><div class="feedaty_widget" data-ver="2021" data-id="61fee7f06c9b2" data-type="merchant" data-variant="150x100-1" data-lang="all" data-gui="all"></div>
Inside react I created a functional component and within useEffect, I created the script.
import { useEffect, useState } from 'react';
const FeedatyButton = () => {
const [scriptLoaded, setScriptLoaded] = useState(false);
useEffect(() => {
const feedatyDiv = document.getElementById('feedatyScript');
if (typeof window !== undefined && !scriptLoaded && !feedatyDiv) {
const script = document.createElement('script');
script.setAttribute('async', '');
script.setAttribute('id', 'feedatyScript');
script.src =
'https://widget.feedaty.com/public/js/feedaty.js?merchant=******&style_ver=2021';
script.addEventListener('load', () => setScriptLoaded(true));
document.appendChild(script);
}
}, []);
return (
<div className='flex justify-end items-center' id='feedatyContainer'>
<div
className='feedaty_widget'
data-ver='2021'
data-id='61fee7f06c9b2'
data-type='merchant'
data-variant='150x100-1'
data-lang='all'
data-gui='all'></div>
</div>
);
};
export default FeedatyButton;
The script loads without problem, but nothing appears inside the feedaty_widget div.
If I create the div with createElement and append it to the document or the body, then it works, but it shows at the end of my react app and obviously, it's not what I want.
I want to place it inside my functional component in a specific position.
What could be the problem?