This is where I created Context
import { createContext } from "react";
const GridContext = createContext([]);
export default GridContext;
this is How I am sending data using context.
const App = () => {
const [grid, setGrid] = useState([]);
useEffect(() => {
const newGrid = getInitialGrid();
setGrid(newGrid);
}, []);
.....
if (grid.length === 0) {
return (
<GridContext.Provider value={grid}>
<div className="App">
<nav className="navbar">
<div>
<h1>Pathfinder Visualizer</h1>
</div>
</nav>
<Info />
<Arena />
</div>
</GridContext.Provider>
);
} else {
return (
<GridContext.Provider value={grid}>
<div className="App">
<nav className="navbar">
<div>
<h1>Pathfinder Visualizer</h1>
</div>
<div className="navitem">
<button className="btn" onClick={() => dijkstra()}>
<span>Visualize</span>
</button>
</div>
</nav>
<Info />
<Arena />
</div>
</GridContext.Provider>
This is where I need the 2d array.
const Arena = () => {
const [grid, setGrid] = useContext(GridContext);
const [mouseIsPressed, setmouseIsPressed] = useState(false);
const [flag, setFlag] = useState(false);
const [start, setStart] = useState([5, 5]);
....
console.log(GridContext._currentValue);
//setGrid(GridContext._currentValue);
console.log(grid);
return <div></div>;
The output of first logger is 2d array while the output of second array is only the first row of 2d array. Also the setGrid is giving error.