如何在使用 mobx 进行反应本机时捕捉屏幕方向变化?

发布于 2025-01-18 17:52:48 字数 705 浏览 2 评论 0原文

您能否向我解释使用MOBX在React Native应用中处理方向更改方向的正确方法。 目前,我尝试过这样的事情:

@observer
export class ChartSTMWork extends Component {
  currwidth = Dimensions.get("window").width;
  render() {
    this.currwidth = Dimensions.get("window").width;
    return();
  }
}

但是,只有在渲染调用时,此代码才会更改Currwidth。在此渲染后我已经渲染表并更改智能手机方向的情况下,Currwidth变量没有影响。

或者,也许我需要使用一些小型计时器来检查尺寸。get(“窗口”)。宽度并使Currwidth可观察到?

 @observer
    export class ChartSTMWork extends Component {
      @observable currwidth = Dimensions.get("window").width;
      someTimerEvery200ms(){
         currwidth = Dimensions.get("window").width
      }
 }

但是该解决方案看起来像是肮脏的解决方案。

我需要如何以正确的方式处理它?

Can you explain me the right way to handle orientation changing in react native app with mobX.
For now I have tried make something like this:

@observer
export class ChartSTMWork extends Component {
  currwidth = Dimensions.get("window").width;
  render() {
    this.currwidth = Dimensions.get("window").width;
    return();
  }
}

But this code change the currWidth only if render call. And in situation when I have already rendered table and change orientation of smartphone after this render, there is no effect to currWidth variable.

Or maybe I need to use some small timer to check Dimensions.get("window").width and make currWidth observable?

 @observer
    export class ChartSTMWork extends Component {
      @observable currwidth = Dimensions.get("window").width;
      someTimerEvery200ms(){
         currwidth = Dimensions.get("window").width
      }
 }

But this solution is looks like dirty solution.

How I need to handle this in right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2025-01-25 17:52:49

按此解法求解。也许这对某人有帮助

  eventToDemension = Dimensions.addEventListener("change", (e) => {
    this.currwidth = e.window.width;
  });
  @observable currwidth = Dimensions.get("window").width;

Solve by this solution. Maybe it's will be heplful for someone

  eventToDemension = Dimensions.addEventListener("change", (e) => {
    this.currwidth = e.window.width;
  });
  @observable currwidth = Dimensions.get("window").width;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文