Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

import a script to my react componetn

import React,{Component} from "react";
import ReactDOM from "react-dom";

import "./styles.css";

class App extends Component {
  componentDidMount() {
    const script = document.createElement("script");    script.async = true;    script.src = "https://some-scripturl.js";    this.div.appendChild(script);  }
  render() {
    return (
      <div className="App" ref={el => (this.div = el)}>        <h1>Hello react</h1>
        {/* Script is inserted here */}
      </div>
    );
  }
}

export default App;
Comment

how to import js via script in react

import { useEffect } from 'react';

const useScript = url => {
  useEffect(() => {
    const script = document.createElement('script');

    script.src = url;
    script.async = true;

    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    }
  }, [url]);
};

export default useScript;
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery document ready function 
Javascript :: chartjs each dataset get colors 
Javascript :: preview upload image js 
Javascript :: jquery 1 cdn 
Javascript :: react google map 
Javascript :: Easy REACT download image 
Javascript :: jest to include text 
Javascript :: electron how to setup preload.js 
Javascript :: javascript insert element after 
Javascript :: how to know which button is clicked in jquery 
Javascript :: alpinejs with select 
Javascript :: Uncaught (in promise) SyntaxError: Unexpected token O in JSON at position 0 
Javascript :: light font color to dark background using javascript 
Javascript :: dm command discord.js 
Javascript :: axios delete request payload 
Javascript :: get first duv with jquery 
Javascript :: how to download file from link in react 
Javascript :: how to change the background color of html in javascript 
Javascript :: jquery select first matching element 
Javascript :: tsconfig 
Javascript :: javascript auto scroll a page to top 
Javascript :: how to sort an array of object 
Javascript :: normalize javascript 
Javascript :: js copy array into another 
Javascript :: how to get connection string value from appsettings.json in .net core 
Javascript :: stylesheet create 
Javascript :: promise.all vs promise.allsettled 
Javascript :: javascript dump strack trace 
Javascript :: how to see node taints 
Javascript :: debug react routes 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =