如何将参数传递给JSX中Redux传奇中的发电机函数?
我有3个生成器函数首先是“ loginuserstart”,其中实际请求到了,然后第二个是“ loginuserasync”,它在“ loginuserstart”中称为api call函数,
所以我试图将参数从我的signin组件传递给我LoginUserStart函数,但是每当我console.log(参数)时,它没有显示任何
代码: -
登录组件
const login = async () => {
arr.userEmail = "sample_email";
arr.userPassword = "sample_password";
console.log(arr);
signinUserStart(arr);
};
const logSubmit = () => {
login();
};
const mapDispatchToProps = (dispatch) => ({
signinUserStart: (data) => dispatch(signinUserStart(data))
});
操作文件代码
export const signinUserStart = (data) => ({
type: UserActionTypes.Set_SigninUser_Start,
payload: data
})
saga文件代码 API发电机功能代码
export async function fetchUser(info) {
console.log(info);
const email = '[email protected]'; //sample_email
// const passwords = info.userPassword;
const password = 'Admin@123'; //sample_password
try {
const user = await axios.post("http://localhost:5050/sign", {
data: {
email: email,
password: password,
},
});
console.log(user);
return user;
} catch (error) {
console.log(error);
return error;
}
}
loginuserasync函数
export function* LoginUserAsync(data) {
console.log("in saga");
console.log(data);
try {
let userInfo = yield call(fetchUser, data)
console.log(userInfo);
yield put(setUserId('62b1c5ee515317d42239066a')); //sample_token
yield put(setCurrentUserName(userInfo.data.userName));
} catch (err) {
console.log(err);
}
}
loginuserstart函数
export function* loginUserStart(action) {
console.log(action.payload);//not logging anything for showing in console
yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync(action));
}
I have 3 generator function first is "loginUserStart" where the actual request comes then the second one is "LoginUserAsync" which is called in the "loginUserStart" and third is api call function
so I am trying to pass the parameter from my signin component to the loginUserStart function but whenever I console.log(arguments) it is showing nothing
Code:-
Sign-in component
const login = async () => {
arr.userEmail = "sample_email";
arr.userPassword = "sample_password";
console.log(arr);
signinUserStart(arr);
};
const logSubmit = () => {
login();
};
const mapDispatchToProps = (dispatch) => ({
signinUserStart: (data) => dispatch(signinUserStart(data))
});
Action file code
export const signinUserStart = (data) => ({
type: UserActionTypes.Set_SigninUser_Start,
payload: data
})
saga File code
API generator function code
export async function fetchUser(info) {
console.log(info);
const email = '[email protected]'; //sample_email
// const passwords = info.userPassword;
const password = 'Admin@123'; //sample_password
try {
const user = await axios.post("http://localhost:5050/sign", {
data: {
email: email,
password: password,
},
});
console.log(user);
return user;
} catch (error) {
console.log(error);
return error;
}
}
LoginUserAsync function
export function* LoginUserAsync(data) {
console.log("in saga");
console.log(data);
try {
let userInfo = yield call(fetchUser, data)
console.log(userInfo);
yield put(setUserId('62b1c5ee515317d42239066a')); //sample_token
yield put(setCurrentUserName(userInfo.data.userName));
} catch (err) {
console.log(err);
}
}
loginUserStart function
export function* loginUserStart(action) {
console.log(action.payload);//not logging anything for showing in console
yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync(action));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能确定没有看到更多代码,但是假设
loginuserstart
是root saga,或者是从root saga开始的,这意味着没有采取任何措施接收。我认为的主要问题是
您要调用的第二个参数中的 这条线“>是错误的,相反,您应该传递传奇本身(作为参考)。
因此,应该像这样:
这样,Redux Saga库将调用
loginuserasync
时,set_signInuser_start
将使用第一个param正确设置为操作对象。I can't be sure without seeing more code, but assuming that
loginUserStart
is either root saga or started from root saga it means there is no action for it to receive.The main issue I think is this line
In the second parameter you are calling the generator function which is wrong, instead you should be passing the saga itself (as reference).
So it should look like this:
This way, the Redux Saga library will then call
LoginUserAsync
whenSet_SigninUser_Start
is dispatched with first param correctly set to the action object.