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

Add how to use react-countup with NextJS 13 app directory #806

Open
jocarrd opened this issue May 1, 2023 · 3 comments
Open

Add how to use react-countup with NextJS 13 app directory #806

jocarrd opened this issue May 1, 2023 · 3 comments

Comments

@jocarrd
Copy link

jocarrd commented May 1, 2023

I think it would be very useful to add in the documentation how to use it in the nextjs server components, at first it took me a bit to understand it and it can help more than one developer.

NextJS doc

Example:

if you try to use it directly within a Server Component, you'll see an error:

import { CountUp } from 'acme-carousel';

export default function Page() {
  return (
    <div>
      <p>Numeric Counter</p>
      <CountUp  enableScrollSpy end={300} duration={3}/>
    </div>
  );
}

This is because Next.js doesn't know is using client-only features.

To fix this, you can wrap third-party components that rely on client-only features in your own Client Components:

 'use client'

import CountUp, { CountUpProps } from 'react-countup'

export const NumberTransition = (props: CountUpProps) => {
  return <CountUp {...props} />
}

Now, you can use directly within a Server Component:

import Carousel from './carousel';

export default function Page() {
  return (
    <div>
      <p>Numeric Counter</p>
      <NumberTransition enableScrollSpy end={300} duration={3} />
    </div>
  );
}
@jocarrd jocarrd changed the title Add how to use the library with NextJS 13 app directory Add how to use react-countup with NextJS 13 app directory May 1, 2023
@liamb13
Copy link

liamb13 commented Aug 9, 2023

I'm unable to get it working at all with Next app directory. It just displays as <span></span>

Update: It's being caused by a completely seperate issue. For some reason, another component using: dangerouslySetInnerHTML is preventing this from running.

@keif
Copy link

keif commented Nov 3, 2023

I was able to get it working with App directory - but you can't use enableScrollSpy - I used the Waypoint component instead.

…but that works in development, but silently fails in a production build, which I'm currently diagnosing.

@Frost7994
Copy link

I was able to get it working with App directory - but you can't use enableScrollSpy - I used the Waypoint component instead.

…but that works in development, but silently fails in a production build, which I'm currently diagnosing.

Any update on this?

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

No branches or pull requests

4 participants