【问题标题】:DeviceOrientation Event on React NativeReact Native 上的 DeviceOrientation 事件
【发布时间】:2020-10-28 20:04:30
【问题描述】:

我正在通过https://www.w3.org/TR/orientation-event/https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation 学习如何在 React Native 应用程序中使用设备方向传感器。

我的 App.js:

const App = () => {
    window.addEventListener("deviceorientation", onOrientationChanged,true);
    function onOrientationChanged(event) {
        
        var absolute = event.absolute;
        var alpha = event.alpha;
        var beta = event.beta;
        var gamma = event.gamma;
        console.log(beta);
    }
        return (
    <>
        <ScrollView
                contentInsetAdjustmentBehavior="automatic">
                <Text>kfkasdfasd</Text>
        </ScrollView>
    </>
  );
};

运行此结果:

TypeError: window.addEventListener is not a function. (In 'window.addEventListener("deviceorientation", onOrientationChanged, true)', 'window.addEventListener' is undefined)

我无法用谷歌搜索任何有同样问题的人,所以当我遵循文档并且没有添加任何额外内容时,我对如何解决这个问题感到困惑。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    React Native 中不存在 window 对象。

    查看 react-native-hooks https://github.com/react-native-community/hooks#usedimensions 中的 useDimension 钩子

    或者如果您想了解更多详情,请查看https://github.com/react-native-sensors/react-native-sensors

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多