Thebuilt-in browser<script> component lets you add a script to your document.
<script> alert("hi!")</script>Reference
<script>
To add inline or external scripts to your document, render thebuilt-in browser<script> component. You can render<script> from any component and React willin certain cases place the corresponding DOM element in the document head and de-duplicate identical scripts.
<script> alert("hi!")</script>
<scriptsrc="script.js" />Props
<script> supports allcommon element props.
It should haveeitherchildren or asrc prop.
children: a string. The source code of an inline script.src: a string. The URL of an external script.
Other supported props:
async: a boolean. Allows the browser to defer execution of the script until the rest of the document has been processed — the preferred behavior for performance.crossOrigin: a string. TheCORS policy to use. Its possible values areanonymousanduse-credentials.fetchPriority: a string. Lets the browser rank scripts in priority when fetching multiple scripts at the same time. Can be"high","low", or"auto"(the default).integrity: a string. A cryptographic hash of the script, toverify its authenticity.noModule: a boolean. Disables the script in browsers that support ES modules — allowing for a fallback script for browsers that do not.nonce: a string. A cryptographicnonce to allow the resource when using a strict Content Security Policy.referrer: a string. Sayswhat Referer header to send when fetching the script and any resources that the script fetches in turn.type: a string. Says whether the script is aclassic script, ES module, or import map.
Props that disable React’sspecial treatment of scripts:
onError: a function. Called when the script fails to load.onLoad: a function. Called when the script finishes being loaded.
Props that arenot recommended for use with React:
blocking: a string. If set to"render", instructs the browser not to render the page until the scriptsheet is loaded. React provides more fine-grained control using Suspense.defer: a string. Prevents the browser from executing the script until the document is done loading. Not compatible with streaming server-rendered components. Use theasyncprop instead.
Special rendering behavior
React can move<script> components to the document’s<head> and de-duplicate identical scripts.
To opt into this behavior, provide thesrc andasync={true} props. React will de-duplicate scripts if they have the samesrc. Theasync prop must be true to allow scripts to be safely moved.
This special treatment comes with two caveats:
- React will ignore changes to props after the script has been rendered. (React will issue a warning in development if this happens.)
- React may leave the script in the DOM even after the component that rendered it has been unmounted. (This has no effect as scripts just execute once when they are inserted into the DOM.)
Usage
Rendering an external script
If a component depends on certain scripts in order to be displayed correctly, you can render a<script> within the component.However, the component might be committed before the script has finished loading.You can start depending on the script content once theload event is fired e.g. by using theonLoad prop.
React will de-duplicate scripts that have the samesrc, inserting only one of them into the DOM even if multiple components render it.
importShowRenderedHTMLfrom'./ShowRenderedHTML.js';functionMap({lat,long}){return(<><scriptasyncsrc="map-api.js"onLoad={()=>console.log('script loaded')}/><divid="map"data-lat={lat}data-long={long}/></>);}exportdefaultfunctionPage(){return(<ShowRenderedHTML><Map/></ShowRenderedHTML>);}
Note
When you want to use a script, it can be beneficial to call thepreinit function. Calling this function may allow the browser to start fetching the script earlier than if you just render a<script> component, for example by sending anHTTP Early Hints response.
Rendering an inline script
To include an inline script, render the<script> component with the script source code as its children. Inline scripts are not de-duplicated or moved to the document<head>.
importShowRenderedHTMLfrom'./ShowRenderedHTML.js';functionTracking(){return(<script> ga('send', 'pageview');</script>);}exportdefaultfunctionPage(){return(<ShowRenderedHTML><h1>My Website</h1><Tracking/><p>Welcome</p></ShowRenderedHTML>);}