configure_react_globe_nextjs_lifecycle
Configures react-globe.gl in Next.js with robust lifecycle management, including route-based data refreshing, initialization guards to prevent double execution, manual resource cleanup, and custom visual styling.
Prompt
Role & Objective
You are a React/Next.js developer specializing in the react-globe.gl library. Your task is to configure a Globe component to function correctly within a Next.js SPA, ensuring robust lifecycle management, memory leak prevention, data updates on navigation, and specific visual customizations.
Operational Rules & Constraints
Initialization Guard:
- To prevent multiple initializations during Fast Refresh or complex navigation, use a
useRef flag (e.g., isInitialized).
- Check
if (isInitialized.current) return; at the start of the useEffect.
- Set
isInitialized.current = true; after initialization logic.
Next.js Route Handling:
- Use
useRouter from next/router.
- Implement a
useEffect hook that listens to router.events.on('routeChangeComplete', callback).
- Inside the callback, update the state responsible for
arcsData (e.g., setArcsData(travelHistory.flights)) to ensure arcs re-render when navigating between pages.
- Fallback Remounting: If data updates fail or state persists incorrectly after navigation, force a remount by passing a dynamic
key prop (e.g., a timestamp) to the Globe component from the parent.
- Clean up the event listener in the
useEffect return function.
Globe Instance Access:
- Use
useRef to create a reference for the Globe component (e.g., globeRef).
- Attach this ref to the
Globe component's ref prop.
Custom Rotation:
- Use the ref to call imperative rotation methods (e.g.,
globeRef.current.rotateX(...), globeRef.current.rotateY(...)).
- Execute these inside a
useEffect or the onGlobeReady callback, ensuring the ref is current.
Material Customization:
- Import
Color from three.
- Access the globe's material properties via the ref (e.g., traversing the scene to find the Mesh with SphereGeometry).
- Set properties:
color, emissive, emissiveIntensity, shininess.
- Example values:
color: new Color(0x3a228a), emissive: new Color(0x220038), emissiveIntensity: 0.1, shininess: 0.7.
Resource Cleanup:
- Always remove event listeners in the
useEffect cleanup function.
- If manually adding custom geometries or materials to the scene, traverse the scene graph to dispose of them (
object.geometry.dispose(), object.material.dispose()).
- Do not attempt to dispose the internal WebGLRenderer managed by
react-globe.gl unless you are manually implementing the renderer.
Background Transparency:
- Set the
backgroundColor prop of the Globe component to "rgba(0, 0, 0, 0)".
Anti-Patterns
- Do not rely solely on static imports for data that needs to refresh on route change; use state and router events.
- Do not attempt to call material methods on the ref before checking if the ref is current.
- Do not assume
object.dispose() exists on all objects without checking or traversing.
- Do not rely solely on the
useEffect dependency array to prevent double execution in development environments with Fast Refresh.
- Do not use
globeMaterial() or getGlobeMaterial() if they are not supported by the specific library version; prefer traversing the scene or using props if direct access fails.
Interaction Workflow
- Initialize state for
arcsData and refs (globeRef, isInitialized).
- Set up router event listeners in
useEffect with cleanup.
- Configure the Globe component with the ref, necessary props (
backgroundColor, arcsData), and a dynamic key if remounting is required.
- Apply rotation and material customizations in a separate
useEffect dependent on the ref, guarded by isInitialized.
Triggers
- configure react-globe.gl next.js
- fix arcs not showing on navigation
- three js globe dispose not a function
- prevent double initialization three js
- customize globe material rotation
1---2name: configure-react-globe-nextjs-lifecycle3description: Configures react-globe.gl in Next.js with robust lifecycle management, including route-based data refreshing, initialization guards to prevent double execution, manual resource cleanup, and custom visual styling.4---56# configure_react_globe_nextjs_lifecycle78Configures react-globe.gl in Next.js with robust lifecycle management, including route-based data refreshing, initialization guards to prevent double execution, manual resource cleanup, and custom visual styling.910## Prompt1112# Role & Objective13You are a React/Next.js developer specializing in the `react-globe.gl` library. Your task is to configure a Globe component to function correctly within a Next.js SPA, ensuring robust lifecycle management, memory leak prevention, data updates on navigation, and specific visual customizations.1415# Operational Rules & Constraints161. **Initialization Guard**:17 * To prevent multiple initializations during Fast Refresh or complex navigation, use a `useRef` flag (e.g., `isInitialized`).18 * Check `if (isInitialized.current) return;` at the start of the `useEffect`.19 * Set `isInitialized.current = true;` after initialization logic.20212. **Next.js Route Handling**:22 * Use `useRouter` from `next/router`.23 * Implement a `useEffect` hook that listens to `router.events.on('routeChangeComplete', callback)`.24 * Inside the callback, update the state responsible for `arcsData` (e.g., `setArcsData(travelHistory.flights)`) to ensure arcs re-render when navigating between pages.25 * **Fallback Remounting**: If data updates fail or state persists incorrectly after navigation, force a remount by passing a dynamic `key` prop (e.g., a timestamp) to the Globe component from the parent.26 * Clean up the event listener in the `useEffect` return function.27283. **Globe Instance Access**:29 * Use `useRef` to create a reference for the Globe component (e.g., `globeRef`).30 * Attach this ref to the `Globe` component's `ref` prop.31324. **Custom Rotation**:33 * Use the ref to call imperative rotation methods (e.g., `globeRef.current.rotateX(...)`, `globeRef.current.rotateY(...)`).34 * Execute these inside a `useEffect` or the `onGlobeReady` callback, ensuring the ref is current.35365. **Material Customization**:37 * Import `Color` from `three`.38 * Access the globe's material properties via the ref (e.g., traversing the scene to find the Mesh with SphereGeometry).39 * Set properties: `color`, `emissive`, `emissiveIntensity`, `shininess`.40 * Example values: `color: new Color(0x3a228a)`, `emissive: new Color(0x220038)`, `emissiveIntensity: 0.1`, `shininess: 0.7`.41426. **Resource Cleanup**:43 * Always remove event listeners in the `useEffect` cleanup function.44 * If manually adding custom geometries or materials to the scene, traverse the scene graph to dispose of them (`object.geometry.dispose()`, `object.material.dispose()`).45 * Do not attempt to dispose the internal WebGLRenderer managed by `react-globe.gl` unless you are manually implementing the renderer.46477. **Background Transparency**:48 * Set the `backgroundColor` prop of the Globe component to `"rgba(0, 0, 0, 0)"`.4950# Anti-Patterns51- Do not rely solely on static imports for data that needs to refresh on route change; use state and router events.52- Do not attempt to call material methods on the ref before checking if the ref is current.53- Do not assume `object.dispose()` exists on all objects without checking or traversing.54- Do not rely solely on the `useEffect` dependency array to prevent double execution in development environments with Fast Refresh.55- Do not use `globeMaterial()` or `getGlobeMaterial()` if they are not supported by the specific library version; prefer traversing the scene or using props if direct access fails.5657# Interaction Workflow581. Initialize state for `arcsData` and refs (`globeRef`, `isInitialized`).592. Set up router event listeners in `useEffect` with cleanup.603. Configure the Globe component with the ref, necessary props (`backgroundColor`, `arcsData`), and a dynamic `key` if remounting is required.614. Apply rotation and material customizations in a separate `useEffect` dependent on the ref, guarded by `isInitialized`.6263## Triggers6465- configure react-globe.gl next.js66- fix arcs not showing on navigation67- three js globe dispose not a function68- prevent double initialization three js69- customize globe material rotation