请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

无法在useEditProject里使用useSearchParams,否则一进入编辑modal过一会就报如下错

图片描述图片描述

export const useUrlQueryParam = <K extends string>(keys: K[]) => {
    const [searchParams] = useSearchParams();
    const setSearchParams = useSetUrlSearchParam();
    const [stateKeys] = useState(keys);
    return [
        useMemo(
            () =>
                subset(Object.fromEntries(searchParams), stateKeys) as {
                    [key in K]: string;
                },
            [searchParams, stateKeys]
        ),
        (params: Partial<{ [key in K]: unknown }>) => {
            return setSearchParams(params);
            // iterator
            // iterator: https://codesandbox.io/s/upbeat-wood-bum3j?file=/src/index.js
        },
    ] as const;
};

export const useSetUrlSearchParam = () => {
    const [searchParams, setSearchParam] = useSearchParams();
    return (params: { [key in string]: unknown }) => {
        const o = cleanObject({
            ...Object.fromEntries(searchParams),
            ...params,
        }) as URLSearchParamsInit;
        return setSearchParam(o);
    };
};

export const useProjectsSearchParams = () => {
    const [param, setParam] = useUrlQueryParam(["name"]);
    return [
        useMemo(
            () => ({ ...param}),
            [param]
        ),
        setParam,
    ] as const;
};

export const useEditProject = () => {
    const client = useHttp();
    const queryClient = useQueryClient();
    const [searchParams] = useProjectsSearchParams()
    console.log(searchParams)
    //const queryKey = ['projects', searchParams]
    const result = useMutation((params: Partial<Project>) => client(`projects/${params.id}`, {
        data: params,
        method: 'PATCH'
    }), {
        onSuccess: () => queryClient.invalidateQueries('projects'),
        // async onMutate(target) {
        //     const previousItems = queryClient.getQueryData(queryKey)
        //     queryClient.setQueryData(queryKey, (old?: Project[]) => {
        //         return old?.map(project => project.id === target.id ? {...project, ...target} : project) || []
        //     })
        //     return {previousItems}
        // },
        // onError(error, newItem, context) {
        //     queryClient.setQueryData(queryKey, (context as { previousItems: Project[] }).previousItems)
        // }

    })
    return result
}

正在回答 回答被采纳积分+3

2回答

瘸腿猫Vivian 2022-09-07 21:10:42

我也是 请问同学解决了吗

0 回复 有任何疑惑可以回复我~
  • 在这节课 抽象出去之前就有问题的 把callback抽象出去use-optimistic-options后 就好了...玄学
    希望有同学或者老师可以讲解一下 为什么抽出去之前按照题主的这个写法 进入编辑会报错  新增没问题 我找了半天 发现useEffect没更新 editingProject的状态 不知道运行到哪一步JS报错了这个.length
    回复 有任何疑惑可以回复我~ 2022-09-07 21:44:05
Nolan 2021-04-22 13:42:16

对比一下源代码吧,看看有什么不一样

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信