javascript - react-native 原生元素检测

标签 javascript android ios react-native

我想在我的 react-native 应用程序中检测用户是否有触摸 ID 传感器,然后如果他有,我想显示带有原生元素操作的按钮,而不仅仅是普通的操作按钮。当我创建 if 语句时,它向我显示错误。我在 expo 客户端 SDK 中使用“create-react-native-app”。

错误消息

enter image description here

代码

class LoginButton extends React.Component {
    state = {
        waiting: false,
    };

    render() {
        let authFunction;
        if (NativeModules.ExponentFingerprint.hasHardwareAsync() === true) {
            if (Platform.OS === 'android') {
                authFunction = async () => {
                    this.setState({waiting: true});
                    try {
                        let result = await NativeModules.ExponentFingerprint.authenticateAsync();
                        if (result.success) {
                            alert('Udało Ci się zalogować')
                        } else {
                            alert('Logowanie nie udane')
                        }
                    }
                    finally {
                        this.setState({waiting: false})
                    }
                };
            } else if (Platform.OS === 'ios') {
                authFunction = async () => {
                    let result = await NativeModules.ExponentFingerprint.authenticateAsync(
                        'Zaloguj się przy użyciu TouchID'
                    );
                    if (result.success) {
                        alert('Udało Ci się zalogować')
                    } else {
                        alert('Logowanie nie udane')
                    }
                };
            }
            return (
                <Button onPress={authFunction} title="Zaloguj się przy użyciu odcisku palca" style={styles.buttonStyle}>
                    {this.state.waiting
                        ? 'Czekam na TouchID...'
                        : 'Zalogowano przy użyciu TouchID'}
                </Button>
            )

        } else if (NativeModules.ExponentFingerprint.hasHardwareAsync() === false) {
            return (
                <Button onPress={} title="Zaloguj się" style={styles.buttonStyle}/>
            )
        }
    }
}

最佳答案

问题在这里

<Button
  onPress={} <--- here
  title="Zaloguj się" 
  style={styles.buttonStyle}
/>

React 不允许您将空表达式分配给 JSX 属性。

为了修复它,只需删除它

<Button title="Zaloguj się" style={styles.buttonStyle}/>

或者将其分配给authFunction,该值将为空。

<Button onPress={authFunction} title="Zaloguj się" style={styles.buttonStyle}/>

关于javascript - react-native 原生元素检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348039/

相关文章:

iphone - CAEmitterCell不遵守持续时间值

android - 从 android sdk 中删除 system.img 是否安全?

android - 不显示 EditText 提示

javascript - 使用 jquery 循环遍历 JSON 对象找不到索引?

javascript - text/plain 和 string 之间有区别吗?

android - 更改android中backstack fragment 的位置?

ios - 错误 404 和 ASIHTTPRequest 下载到文件

iphone - MFMailComposeViewController 已实现,但我仍然收不到邮件

javascript - 为什么 Date.parse 给出不正确的结果?

javascript - Ckeditor 初始禁用小部件按钮状态