分派多个有效载荷
Dispatching more than one payload
我很难理解为什么我不能发送多个负载。在下面的代码中,我想发送“email”和“name”,但是只有第一个 payload(“email”)通过:
dispatch(
createAccount({ email: emailRef.current.value, name: nameUpdate() })
);
下面是接收payload的createSlice:
import { createSlice } from "@reduxjs/toolkit";
import { database, auth } from "../components/firebase-config";
import { ref, set } from "firebase/database";
const initialState = {
value: 0,
};
export const counterSlice = createSlice({
name: "posts",
initialState,
reducers: {
createAccount: (state, action) => {
const user = auth.currentUser;
const uid = user.uid;
set(ref(database, `users/${uid}`), {
email: action.payload.email,
name: action.payload.name,
});
},
},
});
// Action creators are generated for each case reducer function
export const { createAccount } = counterSlice.actions;
export default counterSlice.reducer;
Redux Toolkit 的 createSlice
API 生成的 action creator 默认只接受一个参数作为它们的有效负载。如果你需要传入多个值,你必须把它们都放在一个对象中,比如dispatch(todoAdded({text: 'Buy Milk', completed: false}))
.
鉴于此,您获得的代码看起来 是有效的 atm。如果您可以显示 nameUpdate
函数是什么,那将会有所帮助,但我怀疑它实际上返回的是 undefined
或类似的函数。
我很难理解为什么我不能发送多个负载。在下面的代码中,我想发送“email”和“name”,但是只有第一个 payload(“email”)通过:
dispatch(
createAccount({ email: emailRef.current.value, name: nameUpdate() })
);
下面是接收payload的createSlice:
import { createSlice } from "@reduxjs/toolkit";
import { database, auth } from "../components/firebase-config";
import { ref, set } from "firebase/database";
const initialState = {
value: 0,
};
export const counterSlice = createSlice({
name: "posts",
initialState,
reducers: {
createAccount: (state, action) => {
const user = auth.currentUser;
const uid = user.uid;
set(ref(database, `users/${uid}`), {
email: action.payload.email,
name: action.payload.name,
});
},
},
});
// Action creators are generated for each case reducer function
export const { createAccount } = counterSlice.actions;
export default counterSlice.reducer;
Redux Toolkit 的 createSlice
API 生成的 action creator 默认只接受一个参数作为它们的有效负载。如果你需要传入多个值,你必须把它们都放在一个对象中,比如dispatch(todoAdded({text: 'Buy Milk', completed: false}))
.
鉴于此,您获得的代码看起来 是有效的 atm。如果您可以显示 nameUpdate
函数是什么,那将会有所帮助,但我怀疑它实际上返回的是 undefined
或类似的函数。