In my next.js app I have a page with two components: A
and B
.
// page.tsx
import A from './A'
import B from './B'
const Page = () => {
return (
<div>
<A />
<B />
</div>
)
}
export default Page
// A.tsx
'use client';
import { useState } from 'react'
const A = () => {
const [count, setCount] = useState(0)
return (
<div>
<p>you clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>click me</button>
</div>
)
}
export default A
// B.tsx
'use client';
import { useState } from 'react'
const B = () => {
const [count, setCount] = useState(0)
return (
<div>
<p>you clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>click me</button>
</div>
)
}
export default B
Let's assume that I want to render Page
statically on the server. However, I also would like to implement state on the client side. What is the best way to synchronize the state of A
and B
while keeping Page
as a server component?