Unable to mutate data created by createResource.

I have the following code that i'm trying to get to work but it doesnt update the node/ui
const [tb, { mutate }] = createResource(
() => [params.entityId, toDate()] as const,
([entityId, toDate]) => tbFetch(entityId, toDate),
);
// usage
<TreeView data={tb()!} mutate={mutate} />

<button
class={twclsx('ml-2')}
onClick={async () => {
const searchParams = new URLSearchParams({});
const tree = await apiPromise(
apiFetch(
`/entities/${params.entityId}/reports/master_balances/${props.item.id}?${searchParams.toString()}`,
{
credentials: 'include',
},
),
'Failed to fetch trial balance',
);

if (props.mutate) {
props.mutate!(() => {
props.item.children = flattenTree(tree);
return props.data;
});
}
}}
>
{props.item.name}
</button>
const [tb, { mutate }] = createResource(
() => [params.entityId, toDate()] as const,
([entityId, toDate]) => tbFetch(entityId, toDate),
);
// usage
<TreeView data={tb()!} mutate={mutate} />

<button
class={twclsx('ml-2')}
onClick={async () => {
const searchParams = new URLSearchParams({});
const tree = await apiPromise(
apiFetch(
`/entities/${params.entityId}/reports/master_balances/${props.item.id}?${searchParams.toString()}`,
{
credentials: 'include',
},
),
'Failed to fetch trial balance',
);

if (props.mutate) {
props.mutate!(() => {
props.item.children = flattenTree(tree);
return props.data;
});
}
}}
>
{props.item.name}
</button>
1 Reply
foolswisdom
foolswisdom6mo ago
Can you test by adding an effect logging tb()? That will quickly help determine if there's an issue with how you're setting the data, or with the way reactive dependencies in the tree view are set up