Search code examples
javascriptreactjsreact-hooksmobxmobx-react

Use Mobx Store's value in React Class Component?


I want to access a Hook in a React Class Component.

Konva.tsx

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

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

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

export class Konva extends React.Component {
  stageRef = React.createRef<StageType>();

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

  render() {
    // const frameItStore = useFrameItStore();
    const win: Window = { width: 800, height: 600 }; // frameItStore.win;

    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>
      </>
    );
  }
}

I want to use useFrameItStore() hook which is commented in the above code to set the width & height

store/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,
      box: observable,
      boxCenter: computed,
      trafficSignalPosition: computed,
      trafficSignalColors: observable,
      id: observable
    });

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

  updateWin() {
    if (typeof window === "object") {
      console.log(this.win);
      console.log(window.innerWidth);
      console.log(window.innerHeight);
      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 }
    };
  }
}

store/FrameItContext.tsx

import * as React from "react";
import { useLocalObservable } from "mobx-react";

import { FrameItStore } from "./index";

import { IFrameItStore } from "../types/index";

const FrameItContext = React.createContext<IFrameItStore>(new FrameItStore());

// export const FrameItProvider = ({ children }: { children: React.ReactChild }) => {
//  const frameItStore = useLocalObservable(() => new FrameItStore())

//  return <FrameItContext.Provider value={frameItStore}>{children}</FrameItContext.Provider>
// }

export const useFrameItStore = () => React.useContext(FrameItContext);

However, I can't use Hooks in a Class component. I have made a complete Sandbox → https://codesandbox.io/s/frameit-mobx-konva-ns62n

How do I access the store in the Konva.tsx file?


Solution

  • You can set a contextType on a class component to get access to the context value on this.context.

    export class Konva extends React.Component {
      // ...
    
      static contextType = FrameItContext;
      context!: React.ContextType<typeof FrameItContext>;
    
      render() {
        const { win } = this.context;
    
        // ...
      }
    }