@@ -111,7 +111,7 @@ export const TanStackDevtools = ({
111111 eventBusConfig,
112112} : TanStackDevtoolsReactInit ) : ReactElement | null => {
113113 const devToolRef = useRef < HTMLDivElement > ( null )
114-
114+ const devtoolInstance = useRef < TanStackDevtoolsCore > ( null )
115115 const [ pluginContainers , setPluginContainers ] = useState <
116116 Record < string , HTMLElement >
117117 > ( { } )
@@ -135,23 +135,23 @@ export const TanStackDevtools = ({
135135 typeof plugin . name === 'string'
136136 ? plugin . name
137137 : ( e , theme ) => {
138- const id = e . getAttribute ( 'id' ) !
139- const target = e . ownerDocument . getElementById ( id )
140-
141- if ( target ) {
142- setTitleContainers ( ( prev ) => ( {
143- ...prev ,
144- [ id ] : e ,
145- } ) )
146- }
147-
148- convertRender (
149- plugin . name as PluginRender ,
150- setTitleComponents ,
151- e ,
152- theme ,
153- )
154- } ,
138+ const id = e . getAttribute ( 'id' ) !
139+ const target = e . ownerDocument . getElementById ( id )
140+
141+ if ( target ) {
142+ setTitleContainers ( ( prev ) => ( {
143+ ...prev ,
144+ [ id ] : e ,
145+ } ) )
146+ }
147+
148+ convertRender (
149+ plugin . name as PluginRender ,
150+ setTitleComponents ,
151+ e ,
152+ theme ,
153+ )
154+ } ,
155155 render : ( e , theme ) => {
156156 const id = e . getAttribute ( 'id' ) !
157157 const target = e . ownerDocument . getElementById ( id )
@@ -170,28 +170,34 @@ export const TanStackDevtools = ({
170170 [ plugins ] ,
171171 )
172172
173- const [ devtools ] = useState (
174- ( ) =>
175- new TanStackDevtoolsCore ( {
176- config,
177- eventBusConfig,
178- plugins : pluginsMap ,
179- } ) ,
180- )
173+ // initialize devtools instance
174+ useEffect ( ( ) => {
175+ if ( devtoolInstance . current ) {
176+ return
177+ }
178+ devtoolInstance . current = new TanStackDevtoolsCore ( {
179+ config,
180+ eventBusConfig,
181+ plugins : pluginsMap ,
182+ } )
183+ } , [ config , eventBusConfig , pluginsMap ] )
184+
181185
182186 useEffect ( ( ) => {
183- devtools . setConfig ( {
187+ devtoolInstance . current ? .setConfig ( {
184188 plugins : pluginsMap ,
185189 } )
186- } , [ devtools , pluginsMap ] )
190+ } , [ devtoolInstance , pluginsMap ] )
187191
188192 useEffect ( ( ) => {
189- if ( devToolRef . current ) {
190- devtools . mount ( devToolRef . current )
193+ if ( ! devToolRef . current ) {
194+ return
191195 }
192196
193- return ( ) => devtools . unmount ( )
194- } , [ devtools ] )
197+ devtoolInstance . current ?. mount ( devToolRef . current )
198+
199+ return ( ) => devtoolInstance . current ?. unmount ( )
200+ } , [ devtoolInstance ] )
195201
196202 const hasPlugins =
197203 Object . values ( pluginContainers ) . length > 0 &&
@@ -206,14 +212,14 @@ export const TanStackDevtools = ({
206212
207213 { hasPlugins
208214 ? Object . entries ( pluginContainers ) . map ( ( [ key , pluginContainer ] ) =>
209- createPortal ( < > { PluginComponents [ key ] } </ > , pluginContainer ) ,
210- )
215+ createPortal ( < > { PluginComponents [ key ] } </ > , pluginContainer ) ,
216+ )
211217 : null }
212218
213219 { hasTitles
214220 ? Object . entries ( titleContainers ) . map ( ( [ key , titleContainer ] ) =>
215- createPortal ( < > { TitleComponents [ key ] } </ > , titleContainer ) ,
216- )
221+ createPortal ( < > { TitleComponents [ key ] } </ > , titleContainer ) ,
222+ )
217223 : null }
218224 </ >
219225 )
0 commit comments