简体   繁体   English

在成功回调中调用yield put()

[英]Call yield put() inside a success callback

I am brand new to React and Redux. 我是React和Redux的新手。

I am using react-redux to call the AWS Cognito service which takes an object containing a success and failure callback. 我正在使用react-redux来调用AWS Cognito服务,该服务接受包含成功和失败回调的对象。 I get my JWT back from AWS Cognito when I console.log inside my success callback; 当我在成功回调中的console.log中时,我从AWS Cognito返回了我的JWT; however, how can I yield put() inside this callback since it's not a generator function ( function* ). 但是,如何在这个回调中yield put()因为它不是生成器函数( function* )。

Here's some code: 这是一些代码:

export function* getAWSToken(){
  // username, password and userPool come from react state
  // not showing code for brevity.

  const userData = {
      Username: username,
      Pool: userPool,
    };
  const authenticationData = {
    Username : username,
    Password : password,
  };

  const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

  // This here is the callback
  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess(result){
      yield put(cognitoTokenObtained(result.getIdToken().getJwtToken())
    },
    onFailure(err){}
  });
}

If you're using redux-saga (which is awesome), you can use the call effect to transform an asyc callback like cognitoUser.authenticateUser into a set of instructions to be executed by the middlewhere. 如果您正在使用redux-saga(这很棒),您可以使用调用效果将asyc回调(如cognitoUser.authenticateUser)转换为一组由midwhere执行的指令。

When the middleware resolves the call it will step through the generator to the next yield statement, you can assign the return result to a variable that you can then place in your state using a put effect. 当中间件解析调用时,它将逐步通过生成器到下一个yield语句,您可以将返回结果分配给一个变量,然后可以使用put效果将其置于您的状态。

export function* getAWSToken(){
  // username, password and userPool come from react state
  // not showing code for brevity.

  const userData = {
      Username: username,
      Pool: userPool,
    };
  const authenticationData = {
    Username : username,
    Password : password,
  };

  const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

  // Note: since you're invoking an object method
  // you'll want to pass the object's context (this) with the call via an array

  const token = yield apply(cognitoUser, cognitoUser.authenticateUser, [authenticationDetails, { onSuccess(response){return response}, onFailure(){} }]);

  yield put(cognitoTokenObtained(token.getIdToken().getJwtToken());
}

There's also this incredible tutorial I'd highly recommend. 我强烈推荐这个令人难以置信的教程

Edit: You left out some code for brevity, but I'd highly recommend wrapping the code inside the generator in try catches since you're depending on external IO from an API. 编辑:为了简洁你省略了一些代码,但我强烈建议在try catch中将代码包装在生成器中,因为你依赖于来自API的外部IO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM