【发布时间】:2023-03-08 20:49:01
【问题描述】:
我正在使用 React Apollo 从我的服务器获取数据。当我的页面加载时,我使用 useQuery 来检索数据。这工作正常。问题是当我对搜索表单进行更改时,这会更新导致不需要的重新渲染并再次调用服务器的状态。
我只想在页面加载和单击搜索按钮时调用服务器。
使用查询:
const { loading: loadingLessons, error: lessonsError, data: lessons } = useQuery(
LESSON_BY_PARAMS,
{
variables: { findLessonsInput: searchParams },
},
);
当我更改表单字段时,它会调用 updateFormField 来更新导致重新渲染的 redux 状态
<Autocomplete
options={categories}
getOptionLabel={(option: any) => option.label}
inputValue={form.category}
defaultValue={() => {
const value = categories.find(option => option.label === form.category);
return value;
}}
onChange={(event, value) => updateFormField('category', value?.label)}
renderInput={params => (
<TextField {...params} label="Category" variant="outlined" fullWidth />
)}
/>
我正在使用反应钩子。
【问题讨论】:
标签: reactjs react-redux react-apollo