Skip to content

Commit

Permalink
Refactor and optimize cells component behaviour and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Aug 8, 2022
1 parent e587af0 commit f7d5dff
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/grid/cell/cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const areEqual = (prevProps: CellProps, nextProps: CellProps): boolean =>
);
};

export const CellComponent: FunctionComponent<CellProps> = ({ coords, children, ...restProps }) => {
export const CellComponent: FunctionComponent<CellProps> = React.memo(({ coords, children, ...restProps }) => {
const [mouseDown, onMouseDown, onMouseUp] = useMouseDown();

const onClick = () => restProps.onClick(coords);
Expand Down Expand Up @@ -82,7 +82,7 @@ export const CellComponent: FunctionComponent<CellProps> = ({ coords, children,
};

return <ComponentsMap children={children} {...props} />;
};
}, areEqual);

export const ComponentsMap: FunctionComponent<ComponentsMapProps> = ({ children, ...rest }) => {
const nonActiveCellProps: RevealedProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/GameWithHooks/GameWithHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { Top } from "@components/top-section";
import { openCell } from "@helpers/cells/cells-manipulator";
import { CellState, Coordinates, Field, fieldGenerator, generateFieldWithDefaultState } from "@helpers/field";
import { GameLevels, GameSettings, LevelNames } from "@modules/GameSettings";
import React, { FunctionComponent, useState } from "react";
import React, { FunctionComponent, useMemo, useState } from "react";

export const GameWithHooks: FunctionComponent = () => {
const [level, setLevel] = useState<LevelNames>("beginner");
const [size, bombs] = GameSettings[level];

const [playerField, setPlayerField] = useState<Field>(generateFieldWithDefaultState(size, CellState.hidden));

const gameField = fieldGenerator(size, bombs / (size * size));
const gameField = useMemo(() => fieldGenerator(size, bombs / (size * size)), [size, bombs]);

const onClickHandler = (coords: Coordinates) => {
const newPlayerField = openCell(coords, playerField, gameField);
Expand Down

0 comments on commit f7d5dff

Please sign in to comment.