I am a newbie using Solid JS (and JS altogether for that matter) and I'm facing a seemingly strange behaviour when trying to populate a (custom) <Table>
component using data from an API using fetch()
.
This is my <Table>
:
import { Component, For } from "solid-js";
import "jquery";
import "datatables.net-bs5";
const Table: Component<{ id: string; columns: string[]; data: any[] }> = ({
id,
columns,
data,
}) => {
return (
<>
<div class="table-responsive">
<table id={id} class="table table-striped table-hovr">
<thead>
<For each={columns}>{(col: string) => <th>{col}</th>}</For>
</thead>
<tbody>
<For each={data}>
{(row: {}) => (
<tr>
<For each={Object.values(row)}>
{(d: any) => <td>{d}</td>}
</For>
</tr>
)}
</For>
</tbody>
</table>
</div>
</>
);
};
export default Table;
this is where I want to put the data:
import { Component, createSignal} from 'solid-js';
import Table from '../components/Table';
import "jquery";
import "datatables.net-bs5";
import { getRequest } from '../utils/utils';
import Customer from '../entities/Customer';
const Customers: Component = () => {
const [customers, setCustomers] = createSignal<Customer[]>([]);
const columns = [
'ID Cliente',
'Numero Cliente',
'Azienda?',
'Nome',
'Cognome',
'Sesso',
'Data di nascita',
'Via',
'Città',
'CAP',
'Telefono',
'E-mail'
] /* Display name of the columns */
const req = getRequest("/customers").then((data) => setCustomers(data)
).catch((e) => window.location.href="/");
return <>
<h2>Clienti</h2>
<br />
<Table id="main-table" columns={columns} data={customers()}></Table>
</>
}
export default Customers;
function createStore<T>(arg0: never[]): [any, any] {
throw new Error('Function not implemented.');
}
and this is the function I use to fetch the data:
export async function getRequest(endpoint : string) {
const response = await fetch(config.api_url + endpoint, {
method: "GET",
"headers": {
"Authorization": config.jwt
}
});
return response.json();
}
Basically the problem is that I see the data inside the table on the webpage only the first time after I change something in the <Table>
and save. As I reload the page (and for all other accessess) I only see the <th>
s and an empty table body.
I think I'm misunderstanding the SolidJS Signal, but I'm not sure.
Could you please give me an hint on what am I doing wrong?
Thanks. NC
I tried using the Solid JS Signal without success