Javascript 使用Mobx商店';React类组件中的s值?

Javascript 使用Mobx商店';React类组件中的s值?,javascript,reactjs,react-hooks,mobx,mobx-react,Javascript,Reactjs,React Hooks,Mobx,Mobx React,我想访问React类组件中的钩子 Konva.tsx store/FrameItContext.tsx import*as React from“React”; 从“mobx react”导入{useLocalObservable}; 从“/index”导入{FrameItStore}; 从“./types/index”导入{IFrameItStore}; const FrameItContext=React.createContext(新FrameItStore()); //export co

我想访问React类组件中的钩子

Konva.tsx store/FrameItContext.tsx
import*as React from“React”;
从“mobx react”导入{useLocalObservable};
从“/index”导入{FrameItStore};
从“./types/index”导入{IFrameItStore};
const FrameItContext=React.createContext(新FrameItStore());
//export const FrameItProvider=({children}:{children:React.ReactChild})=>{
//const frameItStore=useLocalObservable(()=>new frameItStore())
//返回{children}
// }
export const useFrameItStore=()=>React.useContext(FrameItContext);
但是,我不能在类组件中使用挂钩。我做了一个完整的沙箱→

如何访问
Konva.tsx
文件中的存储?

您可以设置a以访问
此.context
上的上下文值

导出类Konva.Component{
// ...
静态contextType=FrameItContext;
context!:React.ContextType;
render(){
const{win}=this.context;
// ...
}
}

为什么不将宽度和高度添加为可观察值的一部分,然后根据需要更改FrameItStore.tsx中的值。

您的意思是作为getter?那么如何使用
updateWin
函数?我仍然不能在类组件中使用钩子,对吗?
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>
      </>
    );
  }
}
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 }
    };
  }
}
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);