Home > front end >  how to pass parameter to generator function in redux saga from jsx?
how to pass parameter to generator function in redux saga from jsx?

Time:06-23

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));
}

CodePudding user response:

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 libary will then call LoginUserAsync when Set_SigninUser_Start is dispatched with first param correctly set to the action object.

  • Related