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 :: nested objects javascript 
Javascript :: regex is not empty string 
Javascript :: downgrade node version windows using npm 
Javascript :: days difference in moment js 
Javascript :: get unique array javascript 
Javascript :: remove item from array in jquery 
Javascript :: input clear 
Javascript :: javascript regex generator 
Javascript :: datatable order number 
Javascript :: jquery fadeout and remove 
Javascript :: correct json type 
Javascript :: link stylesheet in javascript 
Javascript :: js date enlever jour 
Javascript :: angular delete with body 
Javascript :: bs modal service close 
Javascript :: javascript code 
Javascript :: data transfer object in node 
Javascript :: iterate through json object 
Javascript :: node.js express post query string 
Javascript :: how to send query parameters in url vuejs 
Javascript :: compare two dates in javascript yyyy-mm-dd 
Javascript :: javascript loop over the alphabet and return the vowels 
Javascript :: math floor 
Javascript :: get first word in javascript 
Javascript :: math.floor js 
Javascript :: react router dom v6 active link 
Javascript :: Uncaught TypeError: Object prototype may only be an Object or null: undefined 
Javascript :: get id of element javascript 
Javascript :: react cheat sheet 
Javascript :: set timeout 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =