Next.js Discord

Discord Forum

Use data from SWR in title?

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I have a page (server component) and a child (client) component. Inside the child client component, I am using SWR to grab some data. I'd like to use a data variable inside my title, but I can't use the new metadata functionality in the client component, so how do I get the data from the child client component to the parent server component so I can set the title dynamically using the data from SWR?

8 Replies

@West African Lion I have a page (server component) and a child (client) component. Inside the child client component, I am using SWR to grab some data. I'd like to use a data variable inside my title, but I can't use the new metadata functionality in the client component, so how do I get the data from the child client component to the parent server component so I can set the title dynamically using the data from SWR?
you can do something like this (this is not documented, but last time i checked it did work)
"use client";
import { useState } from "react";

export default function Page() {
  const [title, setTitle] = useState("Hello World");
  return (
    <div>
      {/* Yes, we simply put the <title> tag anywhere in the document. */}
      <title>{title}</title>
      <input value={title} onChange={e => setTitle(e.target.value)} />
    </div>
  );
}
sorry for the delay, was busy
the reason i think is due to title only accepting one child
i got this error before
West African LionOP
That worked! Thanks!