File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed
src/js/packages/@reactpy/client/src Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -120,21 +120,30 @@ function ScriptElement({ model }: { model: ReactPyVdom }) {
120120 const ref = useRef < HTMLDivElement | null > ( null ) ;
121121
122122 React . useEffect ( ( ) => {
123- if ( ! ref . current ) {
124- return ;
125- }
123+ // Fetch the script's content
126124 const scriptContent = model ?. children ?. filter (
127125 ( value ) : value is string => typeof value == "string" ,
128126 ) [ 0 ] ;
129127
128+ // Don't run if the parent element or script content is missing
129+ if ( ! ref . current || ! scriptContent ) {
130+ return ;
131+ }
132+
133+ // Create the script element
130134 const scriptElement : HTMLScriptElement = document . createElement ( "script" ) ;
131135 for ( const [ k , v ] of Object . entries ( model . attributes || { } ) ) {
132136 scriptElement . setAttribute ( k , v ) ;
133137 }
134- if ( scriptContent ) {
135- scriptElement . appendChild ( document . createTextNode ( scriptContent ) ) ;
136- }
138+
139+ // Append the script content to the script element
140+ scriptElement . appendChild ( document . createTextNode ( scriptContent ) ) ;
137141 ref . current . appendChild ( scriptElement ) ;
142+
143+ // Remove the script element when the component is unmounted
144+ return ( ) => {
145+ ref . current ?. removeChild ( scriptElement ) ;
146+ } ;
138147 } , [ model . key ] ) ;
139148
140149 return < div ref = { ref } /> ;
You can’t perform that action at this time.
0 commit comments