i am sending a data from a page let's call it page1, this data is an array (i tried json it didn't work either). and i want to display the categories that are inside that data in a custom widget but it doesn't work.
i told my self maybe the page isn't even recieving data so i tried recieving it with list widget and it worked so the data is been sent successfully but the custom widget is not working.
so either the custome widget doesn't accepts data that sent from other pages or i am writing a wrong syntax (i tried everything)
here is the code
import React from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
import ReactDOM from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
function CategoryButtonsWidget() {
// Safe initialization (empty array if store.categories is missing)
const [categories, setCategories] = React.useState(
(appsmith.store && appsmith.model.categories) ? appsmith.model.categories : []
);
const [selected, setSelected] = React.useState(null);
React.useEffect(() => {
console.log("Categories from store:", appsmith.store?.categories);
if (appsmith.model && appsmith.model.categories) {
setCategories(appsmith.model.categories);
}
}, []);
const handleClick = React.useCallback((cat) => {
setSelected(cat.id);
appsmith.triggerEvent('onCategoryClick', cat);
}, []);
return (
<div className="category-buttons-widget">
{categories.length === 0 ? (
<p>No categories available</p>
) : (
categories.map((cat) => (
<button
key={cat.id}
className={'category-btn' + (selected === cat.id ? ' active' : '')}
onClick={() => handleClick(cat)}
>
{cat.label}
</button>
))
)}
</div>
);
}
appsmith.onReady(() => {
ReactDOM.render(<CategoryButtonsWidget />, document.getElementById("root"));
});