Tips for verifying the presence of a 3D model from Spline API within the DOM using Next.js

I am in the process of developing a brand new website and I have integrated a 3D model from spline, although it is taking quite some time to load. To improve user experience, I decided to implement a loader/Spinner. However, I'm facing the challenge of determining how to check if the 3D model has finished loading or not.

Below is the code for my component:

import React from "react";
import styles from "../styles/Home.module.css";
import Spline from "@splinetool/react-spline";
import NavBar from "./NavBar";

function WelcomeComp() {
  return (
    <div className={styles.Welcome}>
      <div className="sticky top-4">
        <NavBar />
      </div>
      <div className="flex flex-row h-screen">
        <div className="flex flex-col items-start justify-center">
          <p className={styles.WelcomeLine1}>Hi, My name is Abdallah Zaher</p>
          <p className={styles.WelcomeLine2}>Iam a Front-end developer </p>
        </div>
        <div className="w-1/2">
          <Spline scene="https://prod.spline.design/-----/scene.splinecode" />
        </div>
      </div>
    </div>
  );
}

export default WelcomeComp;

My objective now is to create an "if" condition where the component will display once the model has loaded, otherwise show the spinner:

import WelcomeComp from "../components/WelcomeComp";
import styles from "../styles/Home.module.css";

export default function Home() {
  return (
    <div className={styles.container}>
      <WelcomeComp />
      <div className={styles.loader}></div>
    </div>
  );
}

Answer №1

When using the Spline component, make sure to utilize the onLoad prop in your implementation. Here's an example:

    <Spline 
      onLoad={()=>setLoading(false)} 
      scene="https://prod.spline.design/-----/scene.splinecode" 
    />

For a cleaner setup, consider placing the Spline component and Loader/Spinner component together for easy state management. Here's one way to do it:

    import React,{ useState } from "react";
    import styles from "../styles/Home.module.css";
    import Spline from "@splinetool/react-spline";
    import NavBar from "./NavBar";

    function WelcomeComp() {
      const [loading, setLoading] = useState(true)
      return (
       <>
        {loading && <div className={styles.loader}></div> } // display loader if loading
        <div className={styles.Welcome}>
          <div className="sticky top-4">
            <NavBar />
          </div>
          <div className="flex flex-row h-screen">
            <div className="flex flex-col items-start justify-center">
              <p className={styles.WelcomeLine1}>Hi, My name is Abdallah Zaher</p>
              <p className={styles.WelcomeLine2}>Iam a Front-end developer </p>
            </div>
             <div className="w-1/2">
              <Spline 
               onLoad={()=>setLoading(false)} 
               scene="https://prod.spline.design/-----/scene.splinecode" 
              />
             </div>
          </div>
        </div>
       </>
      );
    }

    export default WelcomeComp;

Don't forget to remove the loader div from the Home component for cleaner code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am getting a null response from my request through the token query

Once the token has been securely stored in Local Storage, I have a function set up to load the data of the current student when the token is verified. However, for some reason, the function is returning null. This is the code for my resolvers: getCurre ...

The colors within my component remain unchanged despite my modifications to the tailwind.config.js file

I have a requirement to create two themes that can change dynamically based on the selected theme. The two available themes are "light" and "dark", and this is how I implement them inside my component: Here is my _app.js: export default function App(appPr ...

Create a webpage layout using Bootstrap that features a left-aligned image within a div, with

Is there a way to achieve this layout using Bootstrap? View the desired design I am looking to create a div with an image on the left (float:left) and text (which could be one or multiple lines) along with a button aligned to the bottom right. The goal i ...

What is the best way to customize the appearance of a mat-checkbox in Angular using Angular Material?

Is there a way to customize the appearance of a mat-checkbox that actually works? I've tried various solutions without success. I attempted to disable view encapsulation and use classes like mdc-checkbox__background, but nothing changed. Even applyin ...

Include parameters for a pagination system

I have a script that fetches data from my database and generates pagination. Everything is working fine, but now I want to include a conditional statement to differentiate the user level as New, Current, or Renewing client. I've already set up some s ...

Tips for properly styling the input type range element

Is there a way to fix the issue with my input type="range" styling using linear-gradient when the variable is set to 75%? It seems to be behaving incorrectly. body{ background: green; } .wrapper { width: 20vmin; } input { widt ...

Interested in retrieving the dynamically changing value of LocalStorage

Hopefully I can articulate my issue clearly. I am implementing a feature where CSS themes change upon button clicks. When a specific theme button is clicked, the corresponding classname is saved to LocalStorage. However, since the key and value in LocalSt ...

Error: The storage module could not be located in the 'firebase/app' package when imported as 'firebase'

Encountered an error when attempting to import Firebase storage: Firebase storage: The 'storage' export (imported as 'firebase') was not found in 'firebase/app' This is my app code: import * as firebase from "firebase/ap ...

Utilize images inputted via an HTML DOM file uploader within p5.js

I'm facing a challenge with allowing users to upload their image file through a DOM file uploader (<input type="file"></input>). Once the image is uploaded, I'm unsure of how to transfer it to JavaScript and process it using p5.js. Ho ...

Tips for running code extracted from a JSON payload

I have a JSON string that contains HTML and JavaScript code. I want to display this code on a page in my React app, but instead of just showing it as a string, I want the HTML and JavaScript to be executed as if it were hard coded. Currently, the code is ...

What is the best way to access the URLs of files within a Google Drive folder within a React application?

I've been working on a react app that's relatively straightforward, but I plan to add a gallery feature in the future. To keep things simple for the 'owner' when updating the gallery without implementing a CMS, I decided to experiment ...

Unable to use text-align property on Material UI Input Component; all other functionalities are functioning correctly

I noticed that when inspecting the code, it appears that all the input styles I've defined are actually applying to the outer div created by Material UI that wraps the Input. However, other styles seem to be working fine, so I'm a bit confused ab ...

Tips for adjusting font size automatically to fit within its parent container every time

On my blog, the post titles are displayed in a div with a video playing in the background. The length of the title varies, ranging from 2 words to over 20 words. When the title is too long, it may overflow from its parent container where the video is playi ...

Avoid allowing users to accidentally double click on JavaScript buttons

I am working with two buttons in a Javascript carousel and need to prevent users from double-clicking on the buttons. Below is the code I am using: var onRightArrow = function(e) { if (unitCtr<=unitTotal) { unitCtr++; TweenLite.to(p ...

My website is experiencing issues with LCP specifically on mobile devices. Instead of receiving a score, I am only presented with a question mark

After developing a website for a client, I noticed that while the desktop score was good, the mobile measurements resulted in an error. This issue could potentially impact the site's performance on Google. I suspect the animations rendered with lottie ...

CreatePortalLink directs users to a payment URL instead of a dashboard

I am currently working on a project that utilizes the Stripe payments extension in conjunction with Firebase. The application is built using Next JS. After a user subscribes, I want to provide them with a tab where they can manage their subscription. The ...

Guide to customizing marker colors on APEXCHARTS in a Next.js project

Is there a way to specify the color of each data point individually? For example, something like { x: 'Yellow', y: [2100, 3000], colors: '#808080' }, as the default color is set for all markers under plotOptions > dumbbellColors. I n ...

Having trouble with your GitHub Pages site displaying properly on desktop but working fine on mobile?

I am facing an issue with my GitHub pages site on a custom domain. It works perfectly fine on mobile, but when I try to access the same URL from desktop, I encounter a DNS_PROBE_FINISHED_NXDOMAIN error. Any suggestions on why this might be happening? You c ...

Running the "npm start" command is resulting in an ELIFECYCLE error with an errno of

After running npm install on my Mac using sudo and encountering 0 vulnerabilities, I faced an error with code ELIFECYCLE and errno 1 when running npm start. However, the same command runs without errors on Windows. Below is the log from the run: 0 info i ...

Ways to insert a gap underneath every line

Hello, I'm struggling to create space below each line of code. I've tried using the br tag and margin property, but it didn't work. Can anyone help me with this? I'm not sure if the current HTML structure is correct or if I should swit ...