I can't seem to to get reed of this error for some reason;
I've looked into it, callbacks, including the code in the useEffect, but it isn't working for me (or I'm doing it wrong).
Here is the code:
import React, { useEffect, useState } from "react";
import "../css/main.css";
import Node from "./node";
const Pathfinder = () => {
const START_NODE_ROW = 10;
const START_NODE_COL = 15;
const FINISH_NODE_ROW = 10;
const FINISH_NODE_COL = 35;
useEffect(() => {
getGrid();
}, []);
const [grid, setGrid] = useState([]);
const getGrid = () => {
setGrid(getInitGrid());
};
const getInitGrid = () => {
const grid = [];
for (let row = 0; row < 20; row++) {
const currentRow = [];
for (let col = 0; col < 50; col++) {
currentRow.push(createNode(col, row));
}
grid.push(currentRow);
}
return grid;
};
const createNode = (col, row) => {
return {
col,
row,
isStart: row === START_NODE_ROW && col === START_NODE_COL,
isFinish: row === FINISH_NODE_ROW && col === FINISH_NODE_COL,
distance: Infinity,
isVisited: false,
isWall: false,
previousNode: null
};
};
return (
<main id="Pathfinder">
{grid.map((row, rowIdx) => {
return (
<div key={rowIdx}>
{row.map((node, nodeIdx) => {
const { row, col, isFinish, isStart } = node;
return (
<Node
key={nodeIdx}
col={col}
isFinish={isFinish}
isStart={isStart}
row={row}
></Node>
);
})}
</div>
);
})}
</main>
);
};
export default Pathfinder;
I'm basically making a grid of Nodes components;
I have to use useEffect because I was trying to make using arrow functions only and hooks, and without classes / react components, which is why I can't use something like componentWillMount.
setGridfunctiongetGridis a really confusing name for a function that callssetGrid. PerhapssetInitialGrid?