简体   繁体   English

为什么当我尝试使用 React Native 钩子时,它无法按预期正常工作?

[英]Why when I try to use react native hooks it does not work properly as intended to be?

I am developing an app using react native, when I try to console.log(useDeviceOrientation());我正在使用 react native 开发应用程序,当我尝试console.log(useDeviceOrientation()); . . The output ( true/false ) during the portrait and landscape did not change.纵向和横向期间的输出( true/false )没有改变。 Could someone help?有人可以帮忙吗?

The library that is used is: @react-native-community/hooks使用的库是:@react-native-community/hooks

API that I used: useDeviceOrientation我使用的 API:useDeviceOrientation

What I try to do:我尝试做的事情:

  1. uninstall the library卸载库
  2. reinstall the same library重新安装同一个库
  3. add dependencies of the library to package.json将库的依赖项添加到 package.json
  4. same problem occurred.发生了同样的问题。 no changes when changing orientation改变方向时没有变化

Code:代码:

// import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { Dimensions, StyleSheet, SafeAreaView, Alert, Button, Platform, StatusBar, View } from 'react-native';
import { useDimensions, useDeviceOrientation } from '@react-native-community/hooks'

export default function App() {
  console.log(useDeviceOrientation());
  const { landscape } = useDeviceOrientation();

  return (
    <SafeAreaView style={styles.container}>
      <View style={{
        backgroundColor: "dodgerblue",
        width: "100%",
        height: landscape ? "100%" : "30%",
      }}
      ></View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0,
    // justifyContent: "center",
    // alignItems: "center",
  },
});

There is no need of 3rd party library for this.为此不需要第 3 方库。 You can simply use this approach.您可以简单地使用这种方法。 First create a functional component named useOrientarion,首先创建一个名为 useOrientarion 的功能组件,

import { useWindowDimensions } from 'react-native';

const useOrientation = () => {
  const height = useWindowDimensions().height;
  const width = useWindowDimensions().width;

  return { isPortrait: height > width };
};

export default useOrientation;

and then in your component,然后在你的组件中,

// import useOrientation
import useOrientation from './useOrientation';

function App() {
  const orientation = useOrientation();
  console.log(orientation);
  // use orientation wherever you want
  // OUTPUT:  {"isPortrait": true} or  {"isPortrait": false}
}

it works for me.这个对我有用。

const { height, width } = useDimensions().window;
const landscape = width > height;

react-native-community 反应本地社区

I believe it is a known issue that they are working on: https://github.com/react-native-community/hooks/issues/210 .我相信这是他们正在处理的一个已知问题: https : //github.com/react-native-community/hooks/issues/210 @ReadRise answer works great as a workaround it though!不过,@ReadRise 答案作为一种解决方法非常有效!

I had the same issue;我遇到过同样的问题; stopping the app and re-building was sufficient to have it fixed.停止应用程序并重新构建足以修复它。

暂无
暂无

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

相关问题 当我尝试在反应中使用 useReducer 钩子时遇到问题 - i have a problem when i try to use useReducer hooks in react 我在菜单项上使用反应钩子。 我的第一次点击没有做任何事情,后续点击按预期工作 - I am using react hooks on menu item. My first click does not do anything, subsequent clicks work as intended 当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有 - Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+” React Native Elevation 阴影无法正常工作 - React Native Elevation shadow does not work properly React Bootstrap 响应式设计无法与 React hooks 和 alan AI 一起使用 - React bootstrap responsive design not work as intended with react hooks and alan AI React 路由器与 React 钩子一起使用时无法正常工作? - React router does not work correctly when used with react hooks? 为什么映射 React 钩子有效,但 lambda 违反钩子规则? - Why does mapping a React hook work, but a lambda violates rule of hooks? 为什么我使用钩子的简单 React 表单不起作用? - Why does my simple React form using hooks not work? 我如何使用带有本机 SSR(没有 NextJS)的反应钩子? - How i can use react hooks with native SSR (without NextJS)? 为什么反应中的匹配媒体无法正常工作? - Why match media in react does not work properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM