I'm converting an HTML theme to Reactjs, I'm having a hard time trying to embed the external Javascript file of that template. I tried appending script to body with useEffect but no success. My problem I think was the function in that Javascript file has executed before DOM render, either way my template is not rendering properly as HTML.
That external script has content of the type:
function(a) {
var b = window.ABC|| {};
b = function() {
function c(c, d) {
var f, e = this;
e.defaults = {
.....
}
var b = 0;
return c
}(),...
I've been searching for days but no results
Edited:
In SingleProduct.js I want images in Product slider work
import React, { useEffect, useLayoutEffect } from 'react';
import RelatedProduct from '../Partials/RelatedProduct';
const SingleProduct = () => {
useLayoutEffect(() => {
document.body.style = 'background: #fff'
}, [])
useEffect(() => {
const script = document.createElement('script')
script.src = '/assets/js/slick-slider.js' // path of the plugin called Slick Slider
script.async = true // false not work too
document.body.append(script) // the element <script> is appended but no work
}, [])
return (
<>
...
The element is appended but no work
The images that should have been in Slider still don't display properly, (they just show up in order)
- Note: I tried to import the js file directly but got an error right away: Expected an assignment or function call and instead saw an expression no-unused-expressions
import '../hooks/slick-slider.min';
One more thing: the external js file plugin can run the first time the website shows up, but when I switch to another route with react-router and come back it doesn't render properly, hard to explain, but I'm sure is that there are many people who have not solved this problem like me.
I have searched and the answers related to the type IIFE function something that does not integrate, but I think there must be a workaround?
The script I'm trying to embed: https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js
I know this plug-in is already on NPM but I would like to try embed it in my website this way and solve my issue.