如何将参数传递给JSX中Redux传奇中的发电机函数?

发布于 2025-02-09 15:48:00 字数 2049 浏览 4 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢好甜 2025-02-16 15:48:00

我不能确定没有看到更多代码,但是假设loginuserstart是root saga,或者是从root saga开始的,这意味着没有采取任何措施接收。

我认为的主要问题是

yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync(action));

您要调用的第二个参数中的 这条线“>是错误的,相反,您应该传递传奇本身(作为参考)。

因此,应该像这样:

yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync);

这样,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

yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync(action));

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:

yield takeLatest(UserActionTypes.Set_SigninUser_Start, LoginUserAsync);

This way, the Redux Saga library will then call LoginUserAsync when Set_SigninUser_Start is dispatched with first param correctly set to the action object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文