Rumah > Soal Jawab > teks badan
Saya meletakkan parameter boolean usingAsSignUp
传递到 queryFn
ke dalam.
Malangnya, usingAsSignUp
总是导致未定义
!我如何获得它的值? usingAsSignUp
是由使用组件中的 useEffect
keadaan tetapan.
Pertanyaan RTK createApi
与 queryFn
:
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;
Status penggunaan 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粉5292450502023-09-14 00:34:54
Anda meluluskan hujah boolean usingAsSignUp
传递给 authenticateWithFirebase
突变端点的 queryFn
, tetapi ia sentiasa menghasilkan undefined< /代码>. Ini mungkin kerana anda tidak menghantar parameter daripada komponen dengan betul.
Untuk membetulkan ralat ini, anda perlu menghantar nilai usingAsSignUp
值作为 signup
传递到 authenticateWithFirebase
端点的 queryFn
在您的 firebaseApi
usingAsSignUp sebagai
await authenticateNow({ email, password, usingAsSignUp: signup });