Unicorn.Studio React Component
raw JSON →The `unicornstudio-react` package provides a community-created React component for embedding interactive 3D scenes designed with Unicorn.Studio into web applications. Currently at version `2.1.4-1`, this library sees frequent updates, often involving bumps to the underlying Unicorn.Studio SDK and fixes for compatibility issues in modern React and Next.js environments. Key differentiators include full TypeScript support, an optimized import path specifically for Next.js applications (leveraging Next.js `Script` and `Image` components), performance optimizations like lazy loading, responsive design, and comprehensive error handling. It allows embedding scenes using either a project ID from Unicorn.Studio's cloud service or a local JSON file. It's important to note that this package is not officially affiliated with Unicorn.Studio and relies on their proprietary script and services.
Common errors
error Warning: Encountered two children with the same key, `__reactFiber$ID`... ↓
unicornstudio-react@2.1.4-1 or newer. Ensure React Strict Mode is compatible or consider adjusting component keys if custom rendering logic is involved. error Unicorn.Studio scene does not load, or a blank canvas is displayed in the browser. ↓
projectId is correctly copied from Unicorn.Studio or that jsonFilePath points to an accessible JSON file. Check the browser console for network errors, JavaScript errors, or CSP violations. Ensure https://cdn.jsdelivr.net and https://assets.unicorn.studio are allowed in your CSP. error Module not found: Can't resolve 'unicornstudio-react/next' ↓
import UnicornScene from 'unicornstudio-react/next';. Ensure npm install unicornstudio-react or yarn add unicornstudio-react was run successfully. If using an older bundler, ensure it supports the exports field in package.json. error ReferenceError: UnicornStudio is not defined (or similar error related to the global UnicornStudio object) ↓
unicornStudio.umd.js) is loading. The component handles script loading automatically, but external factors can interfere. Consider using the onLoad prop to ensure readiness if manually interacting with the SDK or deferring related logic. Warnings
gotcha This package is a community-created wrapper and is not officially affiliated with Unicorn.Studio. Its operation depends on Unicorn.Studio's proprietary script and services, which are external to this package's control. ↓
breaking SDK version bumps can introduce breaking changes in the underlying Unicorn.Studio runtime. For instance, the transition to SDK v2.1.0 included a breaking change for flattened scenes if not updated. ↓
gotcha Prior to version `2.1.4-1`, users in Next.js App Router environments, especially with React Strict Mode enabled, might experience issues such as duplicate canvas rendering or multiple script initializations. This was due to specific handling of script loading and component mounting. ↓
gotcha Using the default import `import UnicornScene from 'unicornstudio-react';` in a Next.js project will bypass the performance optimizations and specialized handling provided by the Next.js-optimized path. This can lead to less efficient script loading and image handling, impacting Lighthouse scores and overall performance. ↓
Install
npm install unicornstudio-react yarn add unicornstudio-react pnpm add unicornstudio-react Imports
- UnicornScene wrong
const UnicornScene = require('unicornstudio-react');correctimport UnicornScene from 'unicornstudio-react'; - UnicornScene (Next.js optimized) wrong
import UnicornScene from 'unicornstudio-react';correctimport UnicornScene from 'unicornstudio-react/next'; - UnicornSceneProps (Type)
import type { UnicornSceneProps } from 'unicornstudio-react';
Quickstart
import { useState } from 'react';
import UnicornScene from 'unicornstudio-react/next';
export default function InteractiveScenePage() {
const [isSceneLoaded, setIsSceneLoaded] = useState(false);
const [error, setError] = useState<Error | null>(null);
const handleLoad = () => {
console.log('Unicorn.Studio scene loaded successfully!');
setIsSceneLoaded(true);
};
const handleError = (err: Error) => {
console.error('Failed to load Unicorn.Studio scene:', err);
setError(err);
};
// Replace 'YOUR_PROJECT_EMBED_ID' with your actual project ID from Unicorn.Studio
// or use jsonFilePath="/path/to/your/scene.json"
const projectId = process.env.NEXT_PUBLIC_UNICORN_STUDIO_PROJECT_ID ?? 'YOUR_PROJECT_EMBED_ID';
return (
<div style={{ width: '100vw', height: '100vh', display: 'flex', justifyContent: 'center', alignItems: 'center', background: '#f0f0f0' }}>
{!isSceneLoaded && !error && <p>Loading interactive scene...</p>}
{error && <p style={{ color: 'red' }}>Error loading scene: {error.message}</p>}
<UnicornScene
projectId={projectId}
width="100%"
height="100%"
scale={0.8}
dpi={2}
lazyLoad={true}
altText="An interactive 3D scene from Unicorn.Studio"
onLoad={handleLoad}
onError={handleError}
style={{ border: '1px solid #ccc', borderRadius: '8px' }}
/>
</div>
);
}