Skip to content

Commit

Permalink
fix: 使用 context
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmengCC committed Sep 19, 2023
1 parent b4b6341 commit 3a60a3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
42 changes: 20 additions & 22 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useCallback, useContext, useState } from "react";
import React, { useCallback, useContext, useReducer, useState } from "react";
import io from "socket.io-client";
import "./App.css";
import PixelGrid from "../PixelGrid/PixelGrid";
import ColorSelect from "../ColorSelect/ColorSelect";
import OnlineCount from "../OnlineCount/OnlineCount";
import { PixelGridContext, initialState, reducer } from "../../stores/store";
// import { produce } from "immer";
// import _ from "lodash";

Expand All @@ -20,38 +21,35 @@ import OnlineCount from "../OnlineCount/OnlineCount";
*/
const socket: any = io("http://localhost:3001");

export const colorContext = React.createContext({
color: "#ff0000",
});

function App() {
const [pixelData, setPixelData] = useState([]);
const [currentColor, setColor] = useState("#ff0000");

const { color } = useContext(colorContext);
const [state, dispatch] = useReducer(reducer, initialState);

const handlePixelClick = (row, col) => {};

const changeCurrentColor = useCallback((color) => {
const changeCurrentColor = (color) => {
setColor(color);
}, []);
};

return (
<React.StrictMode>
<div>
{/* <h1>pixel data</h1> */}
<PixelGrid
onPickColor={changeCurrentColor}
currentColor={currentColor}
onPixelClick={handlePixelClick}
socket={socket}
></PixelGrid>
<ColorSelect
onChange={changeCurrentColor}
color={currentColor}
></ColorSelect>
<OnlineCount socket={socket}></OnlineCount>
<span id="color-pick-placeholder"></span>
<PixelGridContext.Provider value={{ state, dispatch }}>
{/* <h1>pixel data</h1> */}
<PixelGrid
onPickColor={changeCurrentColor}
currentColor={currentColor}
onPixelClick={handlePixelClick}
socket={socket}
></PixelGrid>
<ColorSelect
onChange={changeCurrentColor}
color={currentColor}
></ColorSelect>
<OnlineCount socket={socket}></OnlineCount>
<span id="color-pick-placeholder"></span>
</PixelGridContext.Provider>
</div>
</React.StrictMode>
);
Expand Down
8 changes: 4 additions & 4 deletions src/components/PixelGrid/PixelGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useRef, useState, useEffect, useReducer } from "react";
import React, { useRef, useState, useEffect, useContext } from "react";
import ReactDOM from "react-dom";
import { initialState, reducer } from "../../stores/store";
import { PixelGridContext } from "../../stores/store";
import {
createImageFromArrayBuffer,
getMousePos,
Expand Down Expand Up @@ -31,7 +31,7 @@ interface Props {
function PixelGrid({ onPickColor, currentColor, onPixelClick, socket }: Props) {
const [canvasWidth, setCanvasWidth] = useState(100);
const [canvasHeight, setCanvasHeight] = useState(100);
const [state, dispatch] = useReducer(reducer, initialState);
const { state, dispatch } = useContext(PixelGridContext);

const canvas = useRef<HTMLCanvasElement | null>(null);
const canvasWrapper = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -270,7 +270,7 @@ function PixelGrid({ onPickColor, currentColor, onPixelClick, socket }: Props) {
<canvas
style={{
...canvasStyle,
transform: `scale(${TOP_LAYER})`,
transform: `scale(${zoomLevel})`,
transformOrigin: "top left",
}}
// onWheel={handleZoom}
Expand Down
3 changes: 3 additions & 0 deletions src/stores/store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import { getMousePos } from "../utils";
import {
Zoom,
Expand All @@ -22,6 +23,8 @@ export const initialState = {
draggingRef: null,
};

export const PixelGridContext = React.createContext({} as any);

export function reducer(state, action) {
switch (action.type) {
case Zoom: {
Expand Down

0 comments on commit 3a60a3b

Please sign in to comment.