I have React component (e.g. AGGrid) , can we add react component with all hooks support (e.g. useState, useEffect etc) as a custom element?
I had tried to implement but getting below exception
This is a code of my component
const [rowData] = useState([
{ make: "Toyota", model: "Celica", price: 35000 },
{ make: "Ford", model: "Mondeo", price: 32000 },
{ make: "Porsche", model: "Boxter", price: 72000 }
]);
const [columnDefs] = useState([
{ field: "make" },
{ field: "model" },
{ field: "price" }
]);
return (
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact rowData={rowData} columnDefs={columnDefs}></AgGridReact>
</div>
);
by this im rendering that component as a custom element
return html`
<div
class=${formFieldClasses(gridType, {
errors,
disabled,
readonly,
})}
>
<${Label}
id=${prefixId(id, formId)}
label=${label}
required=${required}
/>
<${GridComp} />
</div>
`;
and getting below exception
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
See Rules of Hooks – React for tips about how to debug and fix this problem.
if i revert the component code to this (Like simply remove UseState hook from component)
function GridComp() {
const [rowData] = [
{ make: "Toyota", model: "Celica", price: 35000 },
{ make: "Ford", model: "Mondeo", price: 32000 },
{ make: "Porsche", model: "Boxter", price: 72000 },
];
const [columnDefs] = [
{ field: "make" },
{ field: "model" },
{ field: "price" },
];
return (
<div className="ag-theme-alpine" style={{ height: 500, width: 600 }}>
<AgGridReact rowData={rowData} columnDefs={columnDefs}></AgGridReact>
</div>
);
}
Then also giving exception.
Cannot add property __, object is not extensible
TypeError: Cannot add property __, object is not extensible
If anyone familier with this would be very helpfull.
Thanks in advance.