Using Server components and error Only plain objects can be passed to Client Components from Server
Answered
North Pacific hake posted this in #help-forum
North Pacific hakeOP
Steps:
1. Create NextJS 14 app with create next app latest.
2. Go in page.js
3. Add Jikan API client and output string to component
4. Error: Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
What? My components are on the server, which I've verified to be true. What is this error?
1. Create NextJS 14 app with create next app latest.
2. Go in page.js
3. Add Jikan API client and output string to component
4. Error: Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
What? My components are on the server, which I've verified to be true. What is this error?
44 Replies
North Pacific hakeOP
page.js
import Image from "next/image";
import styles from "./page.module.css";
import Jikan from 'jikan4.js'
export default function Home() {
console.log('Server only');
const f = typeof window === 'undefined';
console.log(f);
const client = new Jikan.Client();
async function printAnime (id) {
const anime = await client.anime.get(id);
return anime.title;
}
var see = printAnime(6);
return (
<main className={styles.main}>
{see}
</main>
);
}its not so much the api thing, im just utterly confused that this is even possible to be an error in nextjs. this type of code in c# or something would never be a problem.
it would still a problem in any other language since you are passing object between server and client
data has to be serializable
North Pacific hakeOP
this is what i hate about javascript
i thought this is a string
isnt it a string though?
Also what do u mean passing object between server and client, this is a server component. doesnt it render the html on server and thats it, what is it doing client side
@North Pacific hake i thought this is a string
is it a string?
have you console.log it?
Error: Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.Can you send full error message?
you didn't await the promise i think
var see = await printAnime(6);Answer
North Pacific hakeOP
ok so i think ur right about the await
also the thing is this
ContentTitle {
synonyms: [],
english: 'Trigun',
japanese: 'トライガン',
german: null,
spanish: null,
french: null,
default: 'Trigun'
}
synonyms: [],
english: 'Trigun',
japanese: 'トライガン',
german: null,
spanish: null,
french: null,
default: 'Trigun'
}
so its not a string
all of this garbage is avoided because im used to using Visual Studio and C#, an actual sane way to write code
use typescript 😆
North Pacific hakeOP
yes so it works now
ive tried tpyescript
i was extremely disapointed in it
first of all it compiles to javascript which is insane to me
and second its obviously verbose and doesnt and cant ever have the level of IDE support as a statically typed language
anyway thanks guys, so this error was pretty misleading, the issue is not you cant have an object in your component logic, but that you cant have classes in your html, in my mind the output should be either null or object{} appearing on the page not this shitty error
you also can't have an object in your component logic
it doesnt know what to do
unless you stringify it
North Pacific hakeOP
but i mean in the function you can
i literally do in this code
but not in the return part
in the return part you can't directly render objects*
North Pacific hakeOP
yes
i shouldnt call it component logic
I really wish I could just use Blazor, its very good now but React is so popular I have to try to learn it
you should get typescript, as ridiculous it sounds, it helps seasoned developer like you get around with untyped JS monstrosity
North Pacific hakeOP
yes I know, but also when you start using Typescript for a tiny app it seems unnecessary, its only when the app gets bigger that u need it more
actually i use it on every tiny app, its not that bad
id argue when the app gets bigger, TS would get more difficult too, but thats just from my experience
North Pacific hakeOP
mm ok I wish force myself to use it
will*
use it to make using 3rd party library safer, not to make a headache when building own component ðŸ˜