I am building a customer center for my agency and for performance reasons we rely on ssr. I would really like to implement tsparticles, but after what i think is the correct way to set this up, i get this error:
Server Error
Error: Class extends value undefined is not a constructor or null
This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: https://nextjs.org/docs/messages/class-component-in-server-component
The docs say "Warning: This file must be built for client side usage, SSR is not supported." somewhere in between different framework / library setups, so i don't really know what exactly this applies to.
Here are my files:
ParticlesBackrgound.js:
import Particles from "react-tsparticles";
import particlesConfig from "./config/particles-config";
const ParticlesBackground = () => {
retun (
<Particles params={particlesConfig}>
</Particles>
)
}
export default ParticlesBackground
particles-config.js:
const particlesConfig = {
/* coniguraitons */
}
export default particlesConfig
page.jsx:
import Link from 'next/link';
import Image from 'next/image'
import ParticlesBackground from 'src/app/ParticlesBackground.js';
export default function LandingPage() {
return (
<main id="landing-page-wrapper">
<ParticlesBackground/>
<h2>
Customer Center
</h2>
<div id="login">
<button className="strong-hover-shake">Login</button>
</div>
</main>
)
}
I also tried to use transpile inside the next.config.js as i saw it in another issue, but that did not change anything
/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = nextConfig
module.exports = {
transpilePackages: ['tsparticles'],
/* Your Next.js config */
};
As per the above warning, it clearly tells that library doesn't supports Server-side rendering. If you read here on library tsParticles : Its compatible with React.js, NextJS not mentioned.
This library is browser based, you need browser to render it.
https://particles.js.org/docs/index.html
If you read here react-tsparticles is official tsParticles
ReactJS component
. https://github.com/tsparticles/react#readmeHere is a example code I made by using official documentations :
https://www.npmjs.com/package/react-tsparticles#options-object
https://github.com/tsparticles/react#readme
Folder Structure :
Don't forget to install :
npm i tsparticles tsparticles-slim
as this will used to loadloadSlim
orloadFull
.tsparticles to import
loadFull
& tsparticles-slim forloadSlim
Read here about loadSlim :
https://particles.js.org/docs/functions/tsParticles_Slim_Bundle.index.loadSlim.html
Loads the slime bundle with all plugins needed for running the tsParticles Slim package.Read here about loadFull: https://particles.js.org/docs/functions/tsParticles_Full_Bundle.index.loadFull.html Loads the full bundle with all plugins needed for running the tsParticles package.
I have made a
folder part
which has page.js, this page will be available on routehttp://localhost:3000/part
This page has Particles Background.
page.js
loc\src\app\part\page.js
layout.js :
ParticlesBackrgound.js
Don't forget to add your particles config, by using prop
options
.options = {your config}
Don't forget to add this under in your
particlesConfig
:Particles Config : I have passed this options directly (options ={below code}), you may make necessary changes wherever required.
Read More at : https://particles.js.org/docs/classes/tsParticles_Engine.Options_Classes_Options.Options.html