reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.69k stars 1.16k forks source link

[RTK Query] Documentation for `useLazyQuery`'s `data` field #4094

Open JeonJaewon opened 8 months ago

JeonJaewon commented 8 months ago

Hello. As a newbie to rtk-query, I found the official documentation for useLazyQuery a bit confusing while trying to apply it to my project.

In the description, it says the following about data.

Returns the latest request status and cached data from the Redux store

When I first read this description, I thought that data always tries to read the cached values from the store, regardless of whether the trigger is fired or not.

After running it myself, I realized that it was describing the behavior after the trigger.

English isn't my first language, so I'm not 100% sure, but is it just me or is this documentation confusing? If so, is it okay to write a PR to clarify this?

riqts commented 8 months ago

What do you think would be a better wording surrounding this?

It does say immediately below that part:

Note

When the trigger function returned from a LazyQuery is called, it always initiates a new request to the server even if there is cached data. Set preferCacheValue(the second argument to the function) as true if you want it to immediately return a cached value if one exists.

The wording itself in the feature could be considered ambiguous as to whether it is referring to the trigger behavior or the hook as a whole.

I definitely second that some additional clarity could be provided here. If the wording is intentionally left this way it would be nice to have it readily available as to what else outside the trigger is controlling this.

I am under the impression the sentiment is that PRs are always welcome and the docs could always be improved! Might also spark more discussion around it.

JeonJaewon commented 7 months ago

Thanks for the comment! I think the documentation about the behavior of preferCacheValue and trigger that you suggested is a bit of a different topic.

I guess my explanation was not clear enough.

I'm talking about the data in result that corresponds to the second index in the return value of useLazyQuery.

// should data be undefined before trigger()?
const [trigger, { data }] = useLazyQuery()

It seems that data is always undefined before the trigger of that useLazyQuery hook is called - this is true even if there is cached data.

Returns the latest request status and cached data from the Redux store

So I thought the above description in the documentation was ambiguous (and somewhat wrong).

In my opinion, the above wording should be removed. Also, it would be nice to specify that data is initialized since trigger() is called, regardless of whether there is cached data or not.