[在react-admin中,referenceInput对api进行了很多调用-如何避免这种情况?

问题描述 投票:2回答:1

所以我注意到referenceInput多次调用了API。我遇到的问题与:"Too many request to API endpoint when using ReferenceInput and ReferenceArrayInput"

我不确定这是可以避免的事情还是有解决方法。

react-admin
1个回答
0
投票

如果基础输入支持“选择”,那么最好创建自己的组件。例如我创建了一个自定义自动填充输入,您可以从此处进行提示

const AutocompleteProjectInput = ({ name, label, dataProvider }) => {
    const [open, setOpen] = useState(false);
    const [options, setOptions] = useState([]);
    const loading = !open && options.length === 0;

    useEffect(() => {
        let active = true;

        if (!loading) {
            return undefined;
        }

        (async () => {
            const { data: projects } = await dataProvider(GET_LIST, 'projects', {});
            if (active) {
                setOptions(projects);
            }
        })();

        return () => {
            active = false;
        };
    }, [loading]);

    useEffect(() => {
        if (!open) {
            setOptions([]);
        }
    }, [open]);

    return (
        <SelectInput
            source="projectId"
            choices={options}
            optionText="projectName"
            optionValue="id"
            label={label}
        />
    );
};
© www.soinside.com 2019 - 2024. All rights reserved.