将 useEffect 设置的状态参数传递给 RTK Query 的 queryFn
P粉364642019
P粉364642019 2023-09-13 12:37:44
0
1
529

我将布尔参数usingAsSignUp传递到queryFn中。

不幸的是,usingAsSignUp总是导致未定义!我如何获得它的值?usingAsSignUp是由使用组件中的useEffect设置的状态。

RTK 查询createApiqueryFn

export const firebaseApi = createApi({ reducerPath: "firebaseApi", baseQuery: fakeBaseQuery(), tagTypes: ["Auth"], //Optional, https://redux-toolkit.js.org/rtk-query/api/createApi#tagtypes endpoints: (builder) => ({ authenticateWithFirebase: builder.mutation({ async queryFn({ email, password, usingAsSignUp }) { try { const auth = getAuth(firebaseApp); const userCredential = usingAsSignUp ? await createUserWithEmailAndPassword(auth, email, password) : await signInWithEmailAndPassword(auth, email, password); return { data: { uid: userCredential?.user?.uid, email: userCredential?.user?.email, usingAsSignUp: usingAsSignUp, }, }; } catch (e) { return { error: e }; } }, providesTags: ["Auth"], //Optional, https://redux-toolkit.js.org/rtk-query/api/createApi#providestags }), }), }); export const { useAuthenticateWithFirebaseMutation } = firebaseApi;

使用useEffect使用组件来设置传递给queryFn的状态:

import { useAuthenticateWithFirebaseMutation } from "../../persistence/apiSlices"; const [signup, setSignup] = useState(true); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const location = useLocation(); const [authenticateNow, result, data] = useAuthenticateWithFirebaseMutation(); useEffect(() => { location.pathname === "/login" ? setSignup(false) : setSignup(true); }, [location.pathname] ); async function onSubmitACB() { await authenticateNow({ email, password, signup }); }

P粉364642019
P粉364642019

全部回复 (1)
P粉529245050

您正在将布尔参数usingAsSignUp传递给authenticateWithFirebase突变端点的queryFn,但它始终会导致undefined。这可能是因为您没有从组件正确传递参数。

要修复此错误,您需要将usingAsSignUp值作为signup传递到authenticateWithFirebase端点的queryFn在您的firebaseApi配置中。

await authenticateNow({ email, password, usingAsSignUp: signup });
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责声明 Sitemap
    PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!