-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[BUG] useSelect hook ignores staleTime #5843
Comments
I found why the duplication happens. I copied the
When I removed the signal from queryFn, everything worked well. I also found a related ticket about this issue: TanStack/query#6089.
I'm not sure how I can fix this from my project. |
Hey @misterx, sorry for the issue! I've tested in my local using the code you've provided but haven't see anything unusual 🤔 I've observed that the About the |
My investigation has revealed the following issues: Duplicate Requests:
Component Remounting: Caching and Query Cancelation: Behavior Control in Older Versions: @aliemir I've created sandbox https://codesandbox.io/p/devbox/pedantic-albattani-cymd3r where I removed strict mode and see three identical requests. I also created a simple component that shows the input remounting. export const TestInput: React.FC = () => {
const ref = useRef(0);
ref.current++;
console.log("Current ref");
console.log(ref.current);
return <></>;
}; Result in the console (you can see the same in the sandbox): As you can see, the component is created three times. |
Also, one unnecessary render can be removed if https://github.com/refinedev/refine/blob/master/packages/antd/src/hooks/form/useForm.ts#L262-L264 |
Thank you for the investigation @misterx! Linked the wrong PR 🤦 |
@aliemir I would be glad to contribute to the project, but I'm not sure I have enough knowledge to commit since I'm a backend developer and not familiar with frontend development tools such as testing and building tools. After my research, I understand why Refine uses they also load initial values from a request but do not render the form until the values are ready. Currently, I can't use this approach with the existing Refine refine/packages/antd/src/hooks/form/useForm.ts Lines 262 to 264 in 4313d24
So, I've created a simple hack (I'm not sure this is a good solution, but it works) that prevents unnecessary form refreshes on the Edit form until the data is defined. const useFixFormResetOnLoad:(form:FormInstance,initialValues?:any)=>void = (form,initialValues) => {
const originalReset = useRef<FormInstance['resetFields']>(form.resetFields);
useEffect(()=>{
form.resetFields = initialValues===undefined?()=>undefined:originalReset.current;
},[initialValues, form])
} And edit page will be something like: export const BlogPostEdit = () => {
const {form,formProps,saveButtonProps, queryResult, formLoading } = useForm();
useFixFormResetOnLoad(form,queryResult?.data?.data);
if(formLoading){
return (
<div style={{ paddingTop: 50, paddingBottom: 50, textAlign: 'center' }}>
<Spin />
</div>
);
}
return (
<Edit saveButtonProps={saveButtonProps}>
<Form layout="vertical" {...formProps}>
{/*.............................*/}
{/*.... standard refine code ...*/}
{/*.............................*/}
</Form>
</Edit>
} This behavior is good for me because it avoids unnecessary re-rendering for complex forms. However, I'm not sure if this approach will be suitable for others, as form is not visible durring |
@misterx thank you for taking your time for the investigation and explanation. Understood the issue here and your workaround. If I understood correctly, we can try to add a simple condition here at refine/packages/antd/src/hooks/form/useForm.ts Lines 262 to 264 in 4313d24
and then the initial rendering can be handled by users by conditionally rendering via We may also do some hacks in In conclusion, we'll make |
@aliemir Yes, you are correct. Also, since the first and second renders are the same (the state is unchanged and the initialValues are empty), |
Describe the bug
I'm facing the same problem described in #1815, where
useSelect
duplicates requests for Options list, but in this case it ignoresstaleTime
option.Steps To Reproduce
Expected behavior
The options list should be loaded only once.
Packages
"@refinedev/cli": "^2.16.21",
"@refinedev/core": "^4.47.1",
"@refinedev/devtools": "^1.1.32",
"@refinedev/kbar": "^1.3.6",
"@refinedev/simple-rest": "^5.0.1",
"@refinedev/antd": "^5.37.4",
"antd": "^5.0.5",
Additional Context
I've created another simple component,
ResourceUseQuerySelect
, that uses just theuseQuery
hook and makes requests viadatasource.getList
. It seems to have the correct behavior for requesting data.Both components are used on the same page and form.
Upd: I've tried to call
useList
hook in the component, and it duplicates requests too. It looks like there might be something wrong with the useList hook because, under the hood, it uses useQuery, which works fine.I also tested in prod mode, and non strict mode.
The text was updated successfully, but these errors were encountered: