如何动态改变状态的React Native?

如何解决如何动态改变状态的React Native??

开发过程中遇到如何动态改变状态的React Native?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何动态改变状态的React Native?的解决方法建议,希望对你解决如何动态改变状态的React Native?有所启发或帮助;

问题描述

我正在构建一个自定义视图,该视图将根据设备方向旋转其内容。这个应用程式的方向锁定为纵向,我只想旋转一个视图。它获取当前设备方向,更新状态,然后使用更新后的style={{transform: [{rotate: 'xxxdeg'}]}}渲染新组件。

我正在使用react-native-orientation-locker来检测方向变化。

视图渲染在第一个渲染上正确旋转。例如,如果在旋转设备时加载屏幕,它将使视图旋转。但是在更改设备或模拟器的方向时,视图不会旋转。它保持锁定在初始化时的rotate值。

似乎对transform rotate值的更新不会改变旋转。我已验证在渲染过程中是否存在新的rotate值。我已验证方向更改正确更新了状态。但是,当方向改变时,视图永远不会在UI中旋转。就像React Native在渲染期间没有对rotate值所做的更改一样。

我希望对rotate值的更新将相应地使View旋转,但事实并非如此。还有另一种方法可以完成此操作,还是我的代码中有错误?

编辑:rotate是否需要为Animated值?

import React,{useState,useEffect} from 'react';
import {View} from 'react-native';
import Orientation from 'react-native-orientation-locker';

const RotateView = props => {
  const getRotation = newOrientation => {
    switch (newOrientation) {
      case 'LANDSCAPE-LEFT':
        return '90deg';
      case 'LANDSCAPE-RIGHT':
        return '-90deg';
      default:
        return '0deg';
    }
  };

  const [orientation,setOrientation] = useState(
    // set orientation to the initial device orientation
    Orientation.getInitialOrientation(),);
  const [rotate,setRotate] = useState(
    // set rotation to the initial rotation value (xxdeg)
    getRotation(Orientation.getInitialOrientation()),);

  useEffect(() => {
    // Set up listeners for device orientation changes
    Orientation.addDeviceOrientationListener(setOrientation);
    return () => Orientation.removeDeviceOrientationListener(setOrientation);
  },[]);

  useEffect(() => {
    // when orientation changes,update the rotation
    setRotate(getRotation(orientation));
  },[orientation]);

  // render the view with the current rotation value
  return (
    <View style={{transform: [{rotate}]}}>
      {props.children}
    </View>
  );
};

export default RotateView;

尚未找到解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

编程问答相关问答

是否可以将 Python 程序转换为 C/C++? 我需要实现几个算法,我不确定性能差距是否足以证明我在 C/C++ 中执行它时所经历的所有痛苦(我不擅长)).我想写一个简单的算法,并根据这样一个转换后的解决方案对其进行基准测
我想使用 NTT 进行快速平方(请参阅快速 bignum 平方计算),但即使对于非常大的数字……超过 12000 位.
以下代码: myQueue.enqueue(\'a\'); myQueue.enqueue(\'b\'); cout << myQueue.dequeue() << myQueue.dequeue();
据我所知,写时复制不是在 C++11 中实现符合标准的 std::string 的可行方法,但是当它最近在讨论中出现时,我发现我自己无法直接支持这种说法.
这篇文章的评论部分有一个关于使用 std::vector::reserve 的帖子() vs. std::vector::resize().
我了解 inline 本身是对编译器的建议,它可以自行决定是否内联函数,并且还会生成可链接的目标代码.
我最近遇到了一个问题 可以使用模数除法轻松解决,但输入是浮点数: 给定一个周期函数(例如sin)和一个只能在周期范围内计算它的计算机函数(例如[-π,π]),制作一个可以处理任何输入的函数.
我想了解某个函数在我的 C++ 程序中在 Linux 上执行所需的时间.之后,我想做一个速度比较.我看到了几个时间函数,但最终从 boost 得到了这个.时间:
微信公众号搜索 “ 程序精选 ” ,选择关注!
微信公众号搜 "程序精选"关注