I’m a beginner to TypeScript and NextJS and i’ve run into a problem where I can’t pass data to my form due to IntrinsicAttributes Error. This is the error I’m getting.
Type '{ category: Category; }' is not assignable to type 'IntrinsicAttributes'.
Property 'category' does not exist on type 'IntrinsicAttributes'.
I’m trying to send the category to the Edit Category Form but the above error is in the category attribute of EditCategoryForm.
export default async function EditCategoryPage({
params,
}: {
params: { id: string };
}) {
const id = params.id;
const category = await getCategoryById(id);
return (
<main>
...
<EditCategoryForm category={category} />
</main>
);
}
This is my edit category form.
export default function EditCategoryForm({ category }: { category: Category }) {
...
}
This is my Category type.
export type Category = {
id: number;
name: string;
// subcategories: Subcategory[];
subcategories: Subcategory[];
};
This is how I’m fetching the data. The data is fetched properly.
export async function getCategoryById(id: string) {
noStore();
try {
const response = await fetch(API_URL_CATEGORIES + "/" + id + "/");
const body: Category = await response.json();
return body;
} catch (error) {
console.error("Database Error:", error);
throw new Error("Failed to fetch category.");
}
}
I’m unable to see the form due to this error. Am I declaring the props wrong? What is the Intrinsic Attributes error? I’ve passed everything properly though.
2
Answers
Within EditCategoryForm, ensure you’re correctly accessing properties of the category object.
For ex:
<input type="text" value={category.name} />
If using {…category} solves your specific issue, it might be because the category object contains properties that are expected as separate props in your EditCategoryForm component. But it might not always be the best solution.
Ref:
Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes
I have a solution but i am not sure if it will fit your requirements of your project, but i still want to give it a chance.
I have made following changes to your code:
EditCategoryPage.tsx:
App.tsx:
EditCategoryForm.tsx:
This is my placeholder JSON File:
Let me know if you have any trouble/issues with this solution.