Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/nice-turkeys-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@reactlit/core': patch
'@reactlit/radix': patch
'@reactlit/vanilla': patch
---

warn about data fetching with undefined keys and fix bug with old content post-error
6 changes: 5 additions & 1 deletion apps/reactlit-examples/src/mocks/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export type Todo = {
export class TodoService {
constructor(private todos: Todo[], private readonly delay: number = 0) {}

async getTodos() {
async getTodos(fail?: boolean) {
console.log('getTodos');
await wait(this.delay);
if (fail) {
throw new Error('purposefully failed');
}
return this.todos;
}

Expand Down
15 changes: 12 additions & 3 deletions apps/reactlit-examples/src/pages/todo-list/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Callout, Spinner } from '@radix-ui/themes';
import { Button, Callout, Spinner } from '@radix-ui/themes';
import {
DataFetchingPlugin,
defineLayout,
LayoutPlugin,
useReactlit,
} from '@reactlit/core';
import { BoxContainerWrapper, Inputs } from '@reactlit/radix';
import {
BoxContainerWrapper,
DefaultRadixWrapper,
Inputs,
} from '@reactlit/radix';
import { InfoIcon } from 'lucide-react';
import { TodoService } from '../../mocks/todos';

Expand Down Expand Up @@ -37,7 +41,7 @@ const TwoColumnLayout = defineLayout(2, ({ slots: [Slot1, Slot2] }) => {
export default function TodoList() {
const Reactlit = useReactlit(LayoutPlugin, DataFetchingPlugin);
return (
<Reactlit>
<Reactlit wrapper={DefaultRadixWrapper}>
{async ({ display, view, set, changed, fetcher, layout }) => {
display(
<Callout.Root>
Expand All @@ -51,6 +55,11 @@ export default function TodoList() {
</Callout.Root>
);
const todosFetcher = fetcher(['todos'], () => api.getTodos());
display(
<div>
<Button onClick={() => todosFetcher.refetch()}>Refetch</Button>
</div>
);
view(
'adding',
Inputs.AsyncButton(
Expand Down
8 changes: 7 additions & 1 deletion libs/core/src/plugins/data-fetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { definePlugin, ReactlitPlugin } from '../hooks/use-reactlit';

export class DataFetcher<T> {
constructor(
private client: QueryClient,
public client: QueryClient,
private trigger: () => void,
private key: QueryKey,
private fn: () => Promise<T>,
Expand All @@ -20,6 +20,12 @@ export class DataFetcher<T> {
) {}

get() {
if (this.key.some((k) => k === undefined)) {
// eslint-disable-next-line no-console
console.warn(
'One or more of the data fetching keys are undefined, this can result in unexpected behavior'
);
}
const state = this.client.getQueryState(this.key);
if (state?.status === 'error') {
throw state.error;
Expand Down
8 changes: 4 additions & 4 deletions libs/core/src/reactlit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,6 @@ export function Reactlit<T extends StateBase = any>({
debug && console.debug('reactlit rendering:', childArgs.state);
setRenderState(({ elements }) => ({ elements, position: 0 }));
await children(childArgs);
setRenderState(({ elements, position }) => ({
position,
elements: elements.slice(0, position),
}));
} catch (e: any) {
// eslint-disable-next-line no-console
debug && console.error(e);
Expand All @@ -283,6 +279,10 @@ export function Reactlit<T extends StateBase = any>({
)
);
} finally {
setRenderState(({ elements, position }) => ({
position,
elements: elements.slice(0, position),
}));
renderLock.current = false;
if (renderAfter.current) {
renderAfter.current = false;
Expand Down
Loading