-
Notifications
You must be signed in to change notification settings - Fork 17
feat: atomWithQueries #110
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
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
thanks for working on it. i leave the review for @himself65. |
fce993e to
d7786b0
Compare
|
sorry my changes doesnt work but I think we should have a way to make this |
4810bae to
8d1500f
Compare
993a158 to
bb6dd64
Compare
ffc1520 to
f32375a
Compare
|
@himself65 Any other concerns? If there are no issues, I'm ready to complete this PR. |
| const [userIds] = useAtom(userIdsAtom) | ||
|
|
||
| const userQueryAtoms = atomWithQueries({ | ||
| queries: userIds.map((id) => () => ({ | ||
| queryKey: ['user', id], | ||
| queryFn: async ({ queryKey: [, userId] }) => { | ||
| const res = await fetch( | ||
| `https://jsonplaceholder.typicode.com/users/${userId}` | ||
| ) | ||
| return res.json() | ||
| }, | ||
| })), | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerning about the example here, seems like this atom is rely on react hook? can we change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@himself65 The atomWithQueries does not necessarily depend on React hooks; here it is used simply for convenient state management, same as useState.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can change to
const userQueryAtoms = atomWithQueries({
queries: get(userIdsAtom).map(...)
})There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood
| "@tanstack/query-core": "latest", | ||
| "jotai": "latest", | ||
| "jotai-tanstack-query": "latest", | ||
| "jotai-tanstack-query": "../../", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the fix
| const UsersData = () => { | ||
| const [userIds] = useAtom(userIdsAtom) | ||
|
|
||
| const userQueryAtoms = atomWithQueries<User>({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
| queries, | ||
| combine, | ||
| }: { | ||
| queries: Array<(get: Getter) => AtomWithQueryOptions> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, do we have strong type hinting here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@himself65 What of strong type hinting is required?
In the examples, I provided type-safe samples for with and without the combine version.
| const userQueryAtoms = atomWithQueries<User>({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ideal solution is user shouldn't annotate the result type but people can get the type hinting
const atoms = atomWithQueries({
queries: get(userIdsAtom).map(d => ({ userId: '1' }))
})
const users = useAtomValue(atoms)
users[0].userId // <-- without type errorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@himself65 Understood.
Since atomWithQueries returns a compound type of Promise, it is necessary to handle pending and error states when writing code.
const atoms = atomWithQueries({
queries: get(userIdsAtom).map(d => ({ userId: '1' }))
})
const userQueries = useAtomValue(atoms)
if (users[0].isPending) return <>Loading<>
if (users[0].isError) return <>Error<>
users[0].userId // <-- without type errorWe provide a similar type system.
+ const atoms = atomWithQueries<User>({
const atoms = atomWithQueries({| const userQueryAtoms = atomWithQueries<User>({ |
| queryAtom: Atom<AtomWithQueryResult<User>> |
| const UserDisplay = ({ user }: { user: User }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW
Since the project isn't using strong typing yet, I'd suggest making a separate PR just for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I think we can merge this first.
himself65
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feel free to merge when you re ready
|
Could we make a new stable release to include this addition? I'd like to take advantage of this ASAP without having to pin to a preview |
@ThaddeusJiang can you make that, I think I don't have npm access right now. Or you can add NPM_TOKEN in secret and I will try to release though CI |
|
@kvnxiao cc @himself65 |
|
Thanks everyone! |
Implement
atomWithQueriesforuseQueries, resolve #97