I'm struggle with createResource

import { createResource, createEffect } from 'solid-js';

function YourAssignments(props: { me?: typeof users.$inferSelect }) {
// Fetch data using createResource
const [myBoard, { refetch }] = createResource<ResponseBoardsData>(async () => {
try {
const res = await fetch('/api/board/as-assigner');
if (!res.ok) {
throw new Error('Failed to fetch data');
}
const data = await res.json();
console.log('Fetched data:', data);
return data;
} catch (error) {
console.error('Error fetching data:', error);
return { data: [] }; // Return an object with an empty array in case of error
}
});

// Log the data when it changes
createEffect(() => {
console.log('myBoard:', myBoard()?.data); // It's return array
});

return (
<>
{myBoard.loading ? (
<p>Loading...</p>
) : myBoard.error ? (
<p class="notification is-danger">Error: {myBoard.error.message}</p>
) : myBoard() && myBoard().data && myBoard().data.length > 0 ? (
myBoard().data.map((board) => <Board board={board} />) // not a function
) : (
<p class="notification is-info">You have no boards.</p>
)}
</>
);
}
import { createResource, createEffect } from 'solid-js';

function YourAssignments(props: { me?: typeof users.$inferSelect }) {
// Fetch data using createResource
const [myBoard, { refetch }] = createResource<ResponseBoardsData>(async () => {
try {
const res = await fetch('/api/board/as-assigner');
if (!res.ok) {
throw new Error('Failed to fetch data');
}
const data = await res.json();
console.log('Fetched data:', data);
return data;
} catch (error) {
console.error('Error fetching data:', error);
return { data: [] }; // Return an object with an empty array in case of error
}
});

// Log the data when it changes
createEffect(() => {
console.log('myBoard:', myBoard()?.data); // It's return array
});

return (
<>
{myBoard.loading ? (
<p>Loading...</p>
) : myBoard.error ? (
<p class="notification is-danger">Error: {myBoard.error.message}</p>
) : myBoard() && myBoard().data && myBoard().data.length > 0 ? (
myBoard().data.map((board) => <Board board={board} />) // not a function
) : (
<p class="notification is-info">You have no boards.</p>
)}
</>
);
}
I'm noob guys, I thought solidjs could be that easy sorry...
5 Replies
peerreynders
peerreynders2w ago
Not tested, just a sketch:
const boardData = () => {
const data = myBoard()?.data;
if (!data || data.length < 1) return undefined;
return data;
};
return (
<ErrorBoundary
fallback={(error, _reset) => (
<p class="notification is-danger">Error: {error.message}</p>
)}
>
<Suspense fallback={<p>Loading...</p>}>
<Show
when={boardData()}
fallback={<p class="notification is-info">You have no boards.</p>}
>
{(data) => (
<For each={data()}>{(board) => <Board board={board} />}</For>
)}
</Show>
</Suspense>
</ErrorBoundary>
);
const boardData = () => {
const data = myBoard()?.data;
if (!data || data.length < 1) return undefined;
return data;
};
return (
<ErrorBoundary
fallback={(error, _reset) => (
<p class="notification is-danger">Error: {error.message}</p>
)}
>
<Suspense fallback={<p>Loading...</p>}>
<Show
when={boardData()}
fallback={<p class="notification is-info">You have no boards.</p>}
>
{(data) => (
<For each={data()}>{(board) => <Board board={board} />}</For>
)}
</Show>
</Suspense>
</ErrorBoundary>
);
- Show - For - Suspense - ErrorBoundary - Derived signals Just because it uses JSX doesn't mean it's a React clone. The ErrorBoundary can't do its job if you swallow the error in the fetcher.
<Show> - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
<For> - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
<Suspense> - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
Error boundary - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
Derived signals - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
itsZygote
itsZygoteOP2w ago
Not working pretty much but good templating, thanks! Just wonder how reactive function works, whats different between assigning in variable and function
peerreynders
peerreynders2w ago
If you are talking about this:
const boardData = () => {
const data = myBoard()?.data;
if (!data || data.length < 1) return undefined;
return data;
};
const boardData = () => {
const data = myBoard()?.data;
if (!data || data.length < 1) return undefined;
return data;
};
It's used here:
<Show when={boardData()} fallback={}>
<Show when={boardData()} fallback={}>
Solid's JSX is implicitly wrapped in a render effect. - When boardData() runs the first time it also runs myBoard() (a dependency); that automatically subscribes boardData() to be run again once myBoard() changes. - Once myBoard() has changed, boardData() will be rerun for Show which can then change based on its current value. You simply can't do that with plain variables without some kind of compiler magic. For details: Building a Reactive Library from Scratch
DEV Community
Building a Reactive Library from Scratch
In the previous article A Hands-on Introduction to Fine-Grained Reactivity I explain the concepts...
peerreynders
peerreynders2w ago
Not working pretty much
Here is a working example for you to experiment with: https://playground.solidjs.com/anonymous/7a6867fb-3b38-4d38-828c-3c7e9c1561ab (The playground seems to be incredibly sluggish for the first bundling right now)
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
peerreynders
peerreynders2w ago
Version using the updated query/createAsync API available via @solidjs/router now and later standard with Solid 2.x: https://playground.solidjs.com/anonymous/d084d8b4-b955-4d69-8105-f8886a0ab0bc
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template

Did you find this page helpful?