Next.js : Get server component data in client component

Hey, I'm trying the new app dir. It's pretty good, but I'm not sure how to get data from a server component and pass it to my XState machine? 🧐

For now the only way I found is to do it like this
// layout.tsx
export default async function QuoteLayout({ children }: QuoteLayoutProps) {
  const machine = await getMachine() // get a serialized XState machine from a DB

  return (
    <div
      className="container mt-14 w-full max-w-[600px]"
      data-machine={machine}
    >
      {children}
    </div>
  )
}


// machine.ts

const localStorageValue =
  typeof window !== "undefined"
    ? document
        .querySelector<HTMLDivElement>("[data-machine]")
        ?.attributes.getNamedItem("data-machine")?.value
    : null

const stateDefinition = localStorageValue
  ? JSON.parse(localStorageValue)
  : flowMachine.initialState

const previousState = State.create(stateDefinition)

export const FlowService = interpret(flowMachine).start(
  previousState as typeof flowMachine.initialState
)
Was this page helpful?