【问题标题】:React native buttons OnPress does not fire when app running in debug mode当应用程序在调试模式下运行时,反应本机按钮 OnPress 不会触发
【发布时间】:2020-07-26 02:18:24
【问题描述】:

我遇到了一个奇怪的问题。当我在调试模式下运行我的应用程序时,我的按钮中的 onPress 处理程序不会触发。当我停止在调试模式下运行应用程序时,按钮按预期工作。

调试在我的模拟器上有效,但在真实设备上失败。

详情:

OS: Mac
react: "16.11.0"
react-native: "0.62.2"

代码

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React from 'react';
import {
  Button,
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

const App = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <Header />
          {global.HermesInternal == null ? null : (
            <View style={styles.engine}>
              <Text style={styles.footer}>Engine: Hermes</Text>
            </View>
          )}
          <View style={styles.body}>
            <Button
              onPress={() => alert('Does not fire in debug mode')}
              title="Learn More"
              color="#841584"
              accessibilityLabel="Learn more about this purple button"
            />
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Step One</Text>
              <Text style={styles.sectionDescription}>
                Edit <Text style={styles.highlight}>App.js</Text> to change this
                screen and then come back to see your edits.
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>See Your Changes</Text>
              <Text style={styles.sectionDescription}>
                <ReloadInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Debug</Text>
              <Text style={styles.sectionDescription}>
                <DebugInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Learn More</Text>
              <Text style={styles.sectionDescription}>
                Read the docs to discover what to do next:
              </Text>
            </View>
            <LearnMoreLinks />
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;

还有其他人遇到同样的问题吗?

谢谢,

更新:我尝试创建一个新项目,添加一个简单的按钮,然后尝试调试。这个问题是可重现的。

【问题讨论】:

  • 分享你的代码
  • 添加代码sn-p
  • @MithunRaman 你在用脚蹼吗?自从升级到 0.62.2 并使用翻转器后,我在 Android 和 iOS 上得到了同样的结果
  • 我正在使用 RN 调试器。看起来很多人已经面临这个问题。我已经按照这里的评论暂时解决了这个问题:github.com/facebook/react-native/issues/…

标签: reactjs react-native react-native-debugger


【解决方案1】:

对我来说:adb shell "date" &amp;&amp; date. 这导致设备时钟提前约 1 秒。禁用/启用时间同步不起作用。所以我禁用了设备上的同步,然后打开对话框来编辑时间并保存它。这使我的设备落后了几秒钟。这解决了我的问题。


编辑:修复破坏了计时器,即setInterval 没有得到回调...

好吧,我想我需要onPress 比我需要setInterval...

【讨论】:

    【解决方案2】:

    @fadly 的步骤确实对我有用,但不是原样,所以列出来

    1. 通过运行以下命令检查主机和设备之间的日期/时间差异:

    adb shell "日期" && 日期。

    1. 要修复它,请转到设置(设备)-> 日期和时间,禁用自动时间设置,然后重新启用它。

    2. 试试 adb shell "date" && date。应该没有时差。

    注意:如果仍然存在差异,则日期/时间未同步。尝试重置 wifi/移动数据。再次重复上述步骤。它应该可以工作。

    【讨论】:

      【解决方案3】:

      设备上的时间与您的 PC 上的时间不同步 转到PC和设备时间设置并重新启用自动时间设置

      【讨论】:

        【解决方案4】:

        通过运行以下命令确保主机和设备之间没有时差:adb shell "date" &amp;&amp; date

        要修复它,请转到设置(设备)-> 日期和时间,禁用自动时间设置,然后重新启用它。

        【讨论】:

        • iOS 中的相同问题
        【解决方案5】:

        它也发生在我身上,所以在调试时解决真实设备中的问题只需转到你的真实设备设置 => 时间和数据 => 并关闭(切换)自动日期和时间(使用网络提供的时间) 并将其留给手册,然后重新启动应用程序并在 Windows 中按 CTRL + D 进行调试工作,它将像在模拟器中一样工作 注意 => 它适用于模拟器,因为它从我们的系统中获取数据和时间。 希望对你有帮助

        【讨论】:

          猜你喜欢
          • 2019-02-14
          • 1970-01-01
          • 2022-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多