deadcoder0904
deadcoder0904

Reputation: 8683

Listening to Window Resize event in Mobx not updating Stage component's width & height in React Konva

I have a observable win in Mobx which gets updated when the window gets resized. updateWin function updates the win observable.

FrameItStore.tsx

import { makeObservable, observable, action, computed } from "mobx";

import { Point, TrafficSignalPosition, IFrameItStore } from "@/types/index";

export class FrameItStore implements IFrameItStore {
  id = 0;
  win = {
    width: window.innerWidth,
    height: window.innerHeight
  };
  box = {
    width: 1024,
    height: 600
  };
  trafficSignalColors = [
    {
      close: "#EF4444",
      minimize: "#FBBE25",
      maximize: "#49DE80"
    },
    {
      close: "black",
      minimize: "blue",
      maximize: "orange"
    }
  ];

  constructor() {
    makeObservable(this, {
      win: observable,
      updateWin: action.bound,
      box: observable,
      boxCenter: computed,
      trafficSignalPosition: computed,
      trafficSignalColors: observable,
      id: observable
    });

    window.addEventListener("resize", this.updateWin);
  }

  updateWin() {
    this.win.width = window.innerWidth;
    this.win.height = window.innerHeight;
  }

  destroyWin() {
    window.removeEventListener("resize", this.updateWin);
  }

  get boxCenter(): Point {
    return {
      x: (this.win.width - this.box.width) / 2,
      y: (this.win.height - this.box.height) / 2
    };
  }

  get trafficSignalPosition(): TrafficSignalPosition {
    return {
      close: { x: this.boxCenter.x + 20, y: this.boxCenter.y + 20 },
      minimize: { x: this.boxCenter.x + 2 * 20, y: this.boxCenter.y + 20 },
      maximize: { x: this.boxCenter.x + 3 * 20, y: this.boxCenter.y + 20 }
    };
  }
}

The width & height gets updated in the store but I can't see the effect in the component.

If I put console.log(this.win) in the updateWin function, it shows the updated values. But I can't see the updated values in component.

I have put 2 other console.log statements.

URLBar.tsx

import * as React from "react";
import { Rect } from "react-konva";

import { useFrameItStore } from "../store/index";

import { TrafficSignalPosition, Window } from "../types/index";

export const URLBar = () => {
  const frameItStore = useFrameItStore();
  console.log("URLBar.tsx =►", frameItStore.win.width);

  const box: Window = frameItStore.box;
  const trafficSignalPosition: TrafficSignalPosition =
    frameItStore.trafficSignalPosition;

  return (
    <Rect
      x={trafficSignalPosition.maximize.x + 20}
      y={trafficSignalPosition.maximize.y}
      width={box.width - 3 * 20}
      height={20}
      fill="white"
    />
  );
};

Konva.tsx

import * as React from "react";
import { Stage, Layer } from "react-konva";

import { FrameItContext } from "../store/index";
import { BrowserWindow, SiteImage, TrafficSignal, URLBar } from "./index";

import { Stage as StageType } from "konva/types/Stage";

export class Konva extends React.Component {
  static contextType = FrameItContext;
  context!: React.ContextType<typeof FrameItContext>;

  stageRef = React.createRef<StageType>();

  handleExportClick = () => {
    console.log(
      this.stageRef
        .current!.getStage()
        .toDataURL({ mimeType: "image/jpeg", quality: 1 })
    );
  };

  render() {
    const { win } = this.context;
    console.log("Konva.tsx -►", win.width);

    return (
      <>
        <Stage width={win.width} height={win.height} ref={this.stageRef}>
          <Layer>
            <BrowserWindow />
            <URLBar />
            <TrafficSignal />
            <SiteImage />
          </Layer>
        </Stage>
        <button
          style={{ position: "absolute", top: "0" }}
          onClick={this.handleExportClick}
        >
          Download Image
        </button>
      </>
    );
  }
}

But none of the console.log get printed again. It also doesn't update my Konva Stage's width & height so the Stage becomes responsive.

Am I missing anything? A Minimal CodeSandBox is made here. Notice the Console, it doesn't change values.

Upvotes: 1

Views: 1077

Answers (1)

Danila
Danila

Reputation: 18476

You need to wrap your component into observer decorator to make it work with MobX

import { observer } from "mobx-react";

class KonvaComponent extends React.Component {
  // ..
}

export const Konva = observer(KonvaComponent)

or if you use decorator proposal:

import { observer } from "mobx-react";

@observer
export class Konva extends React.Component {
  // ..
}

Upvotes: 3

Related Questions