react-responsive-masonry icon indicating copy to clipboard operation
react-responsive-masonry copied to clipboard

Can't use in Nextjs

Open i-mighty opened this issue 1 year ago • 21 comments

I installed as per the docs and I set in a component within a nextjs project. Expect behavior: The component works as expected. Actual behavior: I get this error ⨯ TypeError: Cannot set property ResponsiveMasonry of #<Object> which has only a getter

i-mighty avatar Jul 29 '24 19:07 i-mighty

same here

AlexM-Val avatar Jul 30 '24 10:07 AlexM-Val

Same for us. Version 2.1.7 and 2.2.2 seem to work fine; 2.3.0 breaks.

smartpuffindev avatar Jul 30 '24 12:07 smartpuffindev

Same here also with Remix v2

eberlinck-soap avatar Jul 30 '24 17:07 eberlinck-soap

Same here with Next 14.2.5 after upgrading to v2.3.0.

narcecl avatar Aug 01 '24 00:08 narcecl

Same.

aaronabf avatar Aug 02 '24 17:08 aaronabf

I am able to use it in Nextjs app router "next": "^14.2.5", "react": "^18.3.1", "react-dom": "^18.3.1", "react-responsive-masonry": "^2.3.0",

'use client'
import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry'
import { FC } from 'react'


const columnsBp = {
  350: 2,
  750: 3,
  992: 4
}

export const SectionMasonryGrid:FC<Props> = ({ data }) => {
	const Items = useMemo(() => getItems(data), [data])

	return (
	    <section>
	        <ResponsiveMasonry columnsCountBreakPoints={columnsBp}>
                    <Masonry gutter={'2vw'}>
                        {Items}
                    </Masonry>
	        </ResponsiveMasonry>
	    </section>
      )
}

The only thing right now is that im having a bug related to hydration which sucks because i want to be SSR but if i use a isMount hook this doesn't happen. Hope it helps.

HectorLS avatar Aug 13 '24 20:08 HectorLS

confirmed that it works on :

    "react-responsive-masonry": "^2.1.7",
    "react": "^18.3.1",
    "next": "^14.2.4",

interestingly something like this fails. Notice the {" "}

image

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.``

hktari avatar Aug 16 '24 10:08 hktari

hello, any workarounds for this?

Ebdulmomen1 avatar Aug 19 '24 08:08 Ebdulmomen1

For now, You should strict version of react-responsive-masonry and also Next.js in my experience to version 13.

"next": "13.1.6",
"react": "18.2.0",
"react-responsive-masonry": "2.1.7",

bashidagha avatar Aug 21 '24 14:08 bashidagha

I am able to use it in Nextjs app router "next": "^14.2.5", "react": "^18.3.1", "react-dom": "^18.3.1", "react-responsive-masonry": "^2.3.0",

'use client'
import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry'
import { FC } from 'react'


const columnsBp = {
  350: 2,
  750: 3,
  992: 4
}

export const SectionMasonryGrid:FC<Props> = ({ data }) => {
	const Items = useMemo(() => getItems(data), [data])

	return (
	    <section>
	        <ResponsiveMasonry columnsCountBreakPoints={columnsBp}>
                    <Masonry gutter={'2vw'}>
                        {Items}
                    </Masonry>
	        </ResponsiveMasonry>
	    </section>
      )
}

The only thing right now is that im having a bug related to hydration which sucks because i want to be SSR but if i use a isMount hook this doesn't happen. Hope it helps.

Hi, how do you use the hook?

wstevenrodriguez avatar Sep 04 '24 16:09 wstevenrodriguez

// components/ui/masonry.ts(x)
'use client';

import dynamic from 'next/dynamic';

export const ResponsiveMasonry = dynamic(() =>
  import('react-responsive-masonry').then((mod) => mod.ResponsiveMasonry),
);
export const Masonry = dynamic(() => import('react-responsive-masonry'));

This have FOUC though so you might want to handle that.

Marviuz avatar Sep 08 '24 13:09 Marviuz

Anyone knows a workaround?

FilipLusnia avatar Sep 09 '24 22:09 FilipLusnia

try using v2.2.0 or dynamic loading

ManigandanRaamanathan avatar Sep 22 '24 14:09 ManigandanRaamanathan

After running this command on node version v20.18.0 solved the issue in my case

npm i [email protected]

my next version is

"next": "13.4.7",

deskofnia avatar Oct 06 '24 15:10 deskofnia

Running npm i [email protected] with next 13.4.7 worked for me as well

hellofadude avatar Oct 06 '24 16:10 hellofadude

I installed as per the docs and I set in a component within a nextjs project. Expect behavior: The component works as expected. Actual behavior: I get this error ⨯ TypeError: Cannot set property ResponsiveMasonry of #<Object> which has only a getter

Im Fix with Dynamic Import:

const ResponsiveMasonry = dynamic(
  () => import("react-responsive-masonry").then((mod) => mod.ResponsiveMasonry),
  {
    loading: () => <p>Loading...</p>,
    ssr: false,
  }
);
const Masonry = dynamic(
  () => import("react-responsive-masonry").then((mod) => mod.default),
  {
    loading: () => <p>Loading...</p>,
    ssr: false,
  }
);

fwebmaster-gt avatar Nov 13 '24 07:11 fwebmaster-gt

I had the exact same issue and can confirm that downgrading to v2.2.0 or using the dynamic loading approach above works 🎉

amadrzyk avatar Dec 13 '24 00:12 amadrzyk

v2.2.0 is also working for me

codeteacher330 avatar Dec 24 '24 12:12 codeteacher330

I'm using nextjs 13. Version ^2.2.0 and 2.6.0 did not work, but 2.2.0 worked for me.

chaudev avatar Dec 27 '24 03:12 chaudev

I was managed to workaround this issue by importing Masonry and ResponsiveMasonry directly. But it will not work correctly and still produce hydration error because it doesn't know window width on server side and cant make masonry list. So the only way is to use only on client

import { ResponsiveMasonryProps, MasonryProps } from 'react-responsive-masonry';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import _Masonry from 'react-responsive-masonry/lib/Masonry';
import _ResponsiveMasonry
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
  from 'react-responsive-masonry/lib/ResponsiveMasonry';

const Masonry: React.FC<MasonryProps> = (props) => <_Masonry {...props} />;
const ResponsiveMasonry: React.FC<ResponsiveMasonryProps> = (props) =>
  <_ResponsiveMasonry {...props} />;

onyshchuk avatar Jan 18 '25 18:01 onyshchuk

Same for us. Version 2.1.7 and 2.2.2 seem to work fine; 2.3.0 breaks.

I installed 2.2.2 version and works for me. Thanks @smartpuffindev

cperazal avatar Feb 07 '25 17:02 cperazal

In my case:

  • next 13.0.6
  • react-responsive-masonry 2.2.0

I solved the problem by upgrading to Nodejs 20 from Nodejs 18

chaudev avatar Mar 24 '25 03:03 chaudev

Downgrade is not a good idea.. What is the point of releasing new version that can't be used? It's a very easy fix, just delete one line of code.

popmanhe avatar Jul 24 '25 05:07 popmanhe