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 }); }
Anda meluluskan hujah boolean
usingAsSignUp
传递给authenticateWithFirebase
突变端点的queryFn
, tetapi ia sentiasa menghasilkanundefined. Ini mungkin kerana anda tidak menghantar parameter daripada komponen dengan betul.
Untuk membetulkan ralat ini, anda perlu menghantar nilai
signup ke 🎜authenticateWithFirebase endpoint 🎜queryFn dalam konfigurasi 🎜firebaseApi anda. 🎜usingAsSignUp
值作为signup
传递到authenticateWithFirebase
端点的queryFn
在您的firebaseApi
usingAsSignUp sebagai