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

What is the best approach for promoting reusability in Angular: creating a new CSS class or a new component?

I have a div with a set of CSS properties that are essential to my application. These properties will be reused across multiple pages and components. <div style="display: flex; flex-direction: column; height: 100%"> //inner html will vary based on c ...

Encountered an unexpected import token error while working with React and Node.js

Encountering an issue while running my react application: Error: Uncaught SyntaxError: Unexpected token import Although I am aware of similar problems discussed elsewhere, I believe mine is slightly different. To start with, here is the link to the reposi ...

The vertical-align property in CSS seems to be malfunctioning

Hi there, I'm struggling with the CSS code below: .parent { position : 'absolute'; top : '50px'; left : '50px'; width : '400px'; height : '160px'; padding : '10px'; ...

What is the process for extracting information from a middleware within a Next.js framework?

I have been working on developing an authentication system using JWT in Next.js. In order to achieve this, I created a middleware that can validate the JWT token and establish an authentication process as shown below: export default function middleware(req ...

Syncing my Tailwind configuration with both my Next.js project and my custom React component library

In my library, the Tailwind classes are not applied unless they are directly used in my project. For example, if I use bg-orange-400 in a Card component within my library, and then import that library into my Next.js project and use the Card component, ...

Is there a way to fix ReactMarkDown when it employs "children" as a prop and throws an error instructing not to pass children as props?

I've encountered an issue with my Next.js app deployment on Vercel. I'm using the ReactMarkDown component to display content fetched from a Strapi backend. Everything works fine on my local machine, but when deploying, I receive the following er ...

Prevent horizontal HTML scrolling while displaying a layer

I am currently working with a .layer div element that darkens the page to highlight a modal. However, I have encountered an issue where upon triggering the event, the div does not occupy 100% of the screen and the original browser scroll bar disappears. I ...

How can I include a close/ok button at the bottom of a bootstrap multiselect component?

Currently, I am utilizing the Bootstrap multiselect tool to filter query outcomes on a specific page. After selecting one or multiple options, my goal is to have a "close" or "OK" button visible at the bottom of the selection list. Upon clicking this butto ...

Having trouble installing dependencies in a React project with TypeScript due to conflicts

I encountered a TypeScript conflict issue whenever I tried to install any dependency in my project. Despite attempting various solutions such as updating dependencies, downgrading them, and re-installing by removing node_modules and package-lock.json, the ...

``How can I prevent browser popups/tooltips from appearing when a user exceeds the maximum value in a number

Is there a way to prevent the tooltip from appearing on the image when a user manually enters a quantity that exceeds the maximum allowed? The tooltip pops up when the form is submitted, and I need to find a way to turn it off. It seems like this tooltip ...

What is the best way to implement a <Toast> using blueprintjs?

Struggling without typescript, I find it quite challenging to utilize the Toast feature. This component appears to have a unique appearance compared to the others. Shown below is an example code. How would you convert this to ES6 equivalent? import { But ...

How to Use Framer Motion in Next.js for window.innerWidth Less Than 768

I am trying to use window.innerWidth to control the timing of an animation (Framer Motion) in my Next.js project, but I keep encountering the error message: ReferenceError: window is not defined Here's a simplified version of my ValuesSection.jsx co ...

Issue with CSS styling on a content slider causing the last picture to display incorrectly

I recently updated a Content Slider and tweaked the styles to my preference. Check out the changes I made on this link. In addition, I included an extra Thumbnail Picture on the right (7th one) along with the Main Picture. Oddly enough, the Slider seems ...

A Step-by-Step Guide on Adding Content After the Bootstrap Slider Using Absolute Position

Check out my project here: I'm currently working on a bootstrap carousel that has absolute positioning and a z-index of -1. However, I'm facing an issue where I can't figure out how to place a div right after the carousel. I've tried s ...

How can React useEffects avoid an infinite loop when using setData()?

const [resourceType, setResourceType] = useState("posts"); const [data, setData] = useState(""); useEffect(() => { console.log("use effects called: " + resourceType); fetch(`https://jsonplaceholder.typicode.com/${resourceType}`) .then((result ...

Functions designed to facilitate communication between systems

There is an interface that is heavily used in the project and there is some recurring logic within it. I feel like the current solution is not efficient and I am looking for a way to encapsulate this logic. Current code: interface Person { status: Sta ...

Resizing images in HTML can sometimes lead to extra white space appearing underneath the

I am facing an unusual scaling issue with my website design. Whenever I resize the browser window horizontally, the image scales correctly, but it leaves a white space below it where the rest of the page should start from if the window is large. I'm ...

How to centrally position an image within a div using Bootstrap

I am a fan of using bootstrap and I recently encountered an issue with applying max-width to an image. It seems that when I do this, the image does not center properly despite using text-center. The solution I found was simply removing the max-width to ...

How to create a bottom border in several TD elements using a combination of CSS and jQuery

Check out my Website: Search Page I have written some CSS that functions well when searching by Name, but not by Specialty: .displayresult { display: block; } #fname, #lname, #splabel, #addlabel, #pnlabel { border-width: 4px; border-bottom-st ...

How can you modify the breakpoint values in a MUI theme to make it responsive?

I'm currently using the v1 beta version of MUI. I've noticed that the media query breakpoints in MUI are different from those in Bootstrap v4, which I am also using. I would like to override the default MUI breakpoint values to align them with Bo ...