mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import React from 'react'
|
|
import Script from 'next/script'
|
|
|
|
const Page = () => {
|
|
const [counter, setCounter] = React.useState(0)
|
|
const [scriptURL, toggleScriptSource] = React.useReducer(
|
|
(currentScriptURL, id) => {
|
|
const scriptSource =
|
|
typeof currentScriptURL === 'string'
|
|
? currentScriptURL
|
|
: currentScriptURL.pathname
|
|
if (scriptSource === '/src-1.js') {
|
|
return new URL(`/src-2.js?key=${Math.random()}`, window.location)
|
|
} else {
|
|
return new URL(`/src-1.js?key=${Math.random()}`, window.location)
|
|
}
|
|
},
|
|
'/src-1.js'
|
|
)
|
|
const scriptSource =
|
|
typeof scriptURL === 'string' ? scriptURL : scriptURL.href
|
|
|
|
return (
|
|
<>
|
|
<Script key={scriptSource} nonce="abc123" src={scriptSource} />
|
|
<h1 id="h1">{'Count ' + counter}</h1>
|
|
<button id="force-rerender" onClick={() => setCounter(counter + 1)}>
|
|
Re-render
|
|
</button>
|
|
<button id="change-script" onClick={toggleScriptSource}>
|
|
Change script src
|
|
</button>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default Page
|