Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spinner isn't not Spinning in Nextjs 14 (Server Side) #576

Open
mahsanr44 opened this issue Nov 8, 2023 · 8 comments
Open

Spinner isn't not Spinning in Nextjs 14 (Server Side) #576

mahsanr44 opened this issue Nov 8, 2023 · 8 comments
Labels

Comments

@mahsanr44
Copy link

mahsanr44 commented Nov 8, 2023

I'm adding react-spinners in my Next.JS app but isn't spinning.
Here's my code:

import { ClimbingBoxLoader} from "react-spinners";
export default function Home() {
  return (
    <div>
<ClimbingBoxLoader 
  color="#36d7b7"
  loading
  speedMultiplier={2}
/>
      <MyComponent />
    </div>
  );
}
@mahsanr44 mahsanr44 changed the title Not Spinning in Nextjs 14 Spinner isn't not Spinning in Nextjs 14 Nov 8, 2023
@mahsanr44 mahsanr44 changed the title Spinner isn't not Spinning in Nextjs 14 Spinner isn't not Spinning in Nextjs 14 (Server Side) Nov 13, 2023
@mahsanr44
Copy link
Author

mahsanr44 commented Nov 13, 2023

I have tested by using use client and it's working fine on the Client Side but I want it to be useful on the Server Side too.

@davidhu2000
Copy link
Owner

i'll need to spent some time looking into how to handle the new server component paradigm. I think it's probably not working because the animation are created outside of the component

https://github.com/davidhu2000/react-spinners/blob/main/src/ClimbingBoxLoader.tsx#L7-L21

so maybe that code isn't executed after the code is sent to the client

@mahsanr44
Copy link
Author

Yeah, you're right, this could be the one of reasons.
Take your time and fix it.
Thanks

@milindgoel15
Copy link

client-side animation stuff cannot be rendered on the server. They need to be marked as client component to work. Its not something that's fixable imo.

@JDuqn
Copy link

JDuqn commented Mar 6, 2024

For now I just force all my loaders to be client components with a fallback for server side :

"use client"

import React, { useEffect, useState } from "react"
import { ClipLoader } from "react-spinners"

export const Loader = ({ size }: { size: number }) => {
  const [isClient, setIsClient] = useState(false)

  useEffect(() => {
    setIsClient(true)
  }, [])

  return (
    <React.Fragment>
      {isClient ? (
        <ClipLoader ... />
      ) : (
        <ServerSideSpinner ... />
      )}
    </React.Fragment>
  )
}

ServerSideSpinner is a regular spinner animated with tailwind.

Not sure this is a good approach, but it might help someone.

@milindgoel15
Copy link

Don't do this, make a separate file, add "use client" tag on top, import the spinner and immediately export it as const or default (,ur choice) and import this spinner in your pages.

@davidhu2000
Copy link
Owner

davidhu2000 commented Mar 6, 2024

i haven't had time to work on this recently, but one potential approach would be add use client on top of each loader, and add a setState call for each animation. This ensures it will run on the client.

const [animation] = useState(createAnimation(...))

a quick test locally does seem to work, just need to update it all. Maybe hack on it in a coming weekend.

i thought maybe using next dynamic could do it, but the create Aniamtion still doesn't run on the client

const Loader = dynamic(() => import("react-spinners/ClimbingBoxLoader"), { ssr: false });

@JDuqn
Copy link

JDuqn commented Mar 6, 2024

const [animation] = useState(createAnimation(...))

This will execute createAnimation on every render uselessly, I would advise using a callback

const [animation] = useState(() => createAnimation(...))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants