【问题标题】:How to use yield call in React-Native?如何在 React-Native 中使用 yield 调用?
【发布时间】:2020-04-08 02:01:18
【问题描述】:

我正在我的 RN 应用程序中实现指纹扫描,我找到了一个很好的教程,但是那里的代码有一个我从未使用过的语法 - yield call(),但是,我用谷歌搜索它并找不到正确的解释为了它。 代码如下:

if (isFingerPrintSupported === true) {
                        yield call(KeychainService.setCredentials, user_name, 
                                          JSON.stringify({ password }));
                    }

在这种情况下我可以用其他东西代替吗?如果没有,我该如何导入或安装以使其正常工作?

编辑(添加示例代码):

   componentWillMount() {
    let credentials = yield call(KeychainService.getCredentials);
    if (credentials && credentials.username)) {
        let isFingerPrintSupported = yield call(KeychainService.checkBiometricSupportednEnrolled);

        if (isFingerPrintSupported === true) {
            // show fingerprint alert on login page
            // and authenticate FingerPrint when user touch the sensor
        }
    } else {
        // else don’t show fingerprint option on login
    }
}

【问题讨论】:

    标签: react-native call redux-saga yield


    【解决方案1】:

    yield 可以在生成器函数中使用,它有助于随时异步暂停和恢复函数。此外,它还有助于从生成器函数返回值。

    查看document 了解更多信息。

    call 是 redux-saga 的效果,有助于进行异步调用。更多信息请查看this

    import { call } from 'redux-saga/effects'
    
    function* authorize(user, password) {
      try {
        const response = yield call(/** Api call */, user, password)
        ...
      } catch(error) {
        ...
      }
    }
    

    注意

    如果你不想使用yield,你可以使用axiosfetch直接调用你的API。

    希望这对您有所帮助。欢迎提出疑问。

    【讨论】:

    • 查看我编辑的问题并给我一个建议如何使用 fetch 代替。
    • 我真的需要为 Yield 导入一些东西吗?
    • yield 应该在生成器函数中使用。另外,您需要导入 redux-saga 效果才能使用call
    • 我是否在编辑代码中正确使用它?当我将鼠标悬停在 Yield 语法上时,它不显示任何详细信息,似乎我需要导入一些东西
    • @bmmf 我会检查让你知道的。
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2019-06-30
    • 2019-02-17
    • 2020-05-07
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多