google analytics
'use client';
// import { getServerSession } from 'next-auth';
import ProductCard from './components/ProductCard/ProductCard';
// import { authOptions } from './api/auth/[...nextauth]/route';
import HeavyComponent from './components/HeavyComponent';
import { useState } from 'react';
export default function Home() {
const [isVisible, setVisible] = useState(false);
// const session = await getServerSession(authOptions);
return (
<main>
<h1>Hello World</h1>
<button onClick={() => setVisible(true)}>Show</button>
{isVisible && <HeavyComponent />}
<ProductCard />
</main>
);
}
dynamic
리액트의 다이내믹 임포트랑 비슷한 것 같다.
https://medium.com/@shubham3480/dynamic-imports-in-react-3e3e7ad1d210
'use client';
import { useState } from 'react';
// 수정된 부분
import dynamic from 'next/dynamic';
const HeavyComponent = dynamic(() => import('./components/HeavyComponent'), {
// browser API를 사용했을 경우, SSR은 오류가 난다. 그 때 쓸 수 있는 설정
ssr: false,
// 아래와 같이 로딩을 추가할 수도 있다.
loading: () => <p>Loading...</p>,
});
export default function Home() {
const [isVisible, setVisible] = useState(false);
return (
<main>
<h1>Hello World</h1>
<button onClick={() => setVisible(true)}>Show</button>
{isVisible && <HeavyComponent />}
</main>
);
}
❓서버 컴포넌트는 왜 안 받아와지지? (use client를 써야하는 이유)
기존에는 전체 라이브러리를 page.js 번들에 포함시킨다.
// 기존
'use client';
import { useState } from 'react';
import dynamic from 'next/dynamic';
import _ from 'lodash';
export default function Home() {
const [isVisible, setVisible] = useState(false);
return (
<main>
<h1>Hello World</h1>
<button
onClick={() => {
const users = [{ name: 'c' }, { name: 'b' }, { name: 'a' }];
const sorted = _.orderBy(users, ['name']);
console.log(sorted);
}}
>
Show
</button>
</main>
);
}