Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLiveQuery #2035

Open
zj91zj opened this issue Jul 12, 2024 · 0 comments
Open

useLiveQuery #2035

zj91zj opened this issue Jul 12, 2024 · 0 comments

Comments

@zj91zj
Copy link

zj91zj commented Jul 12, 2024

"react": "^18.2.0",
"dexie": "^4.0.7",
"dexie-react-hooks": "^1.1.7"
报错TypeError: Cannot read properties of null (reading 'useRef')

import React from 'react';
import { Dexie, type EntityTable } from 'dexie';
import { useLiveQuery } from 'dexie-react-hooks';

// Typing for your entities (hint is to move this to its own module)
export interface Friend {
id: number;
name: string;
age: number;
}

// Database declaration (move this to its own module also)
export const db = new Dexie('FriendDatabase') as Dexie & {
friends: EntityTable<Friend, 'id'>;
};
db.version(1).stores({
friends: '++id, age',
});

// Component:
export function MyDexieReactComponent() {
const youngFriends = useLiveQuery(() =>
db.friends
.where('age')
.below(30)
.toArray()
);

return (
<>

My young friends



    {youngFriends?.map((f) => (

  • Name: {f.name}, Age: {f.age}

  • ))}

<button
onClick={() => {
db.friends.add({ name: 'Alice', age: 21 });
}}
>
Add another friend

</>
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant