I have some code that makes an array of div elements to place inside a CSS grid, which is rendered via React since that's extremely fast after the first render. I want to be able to interact with those elements via multiple components, so I set up some context that stores the state of the grid. I'm running into a problem now where I want to set an onClick function on those grid elements so that when they are clicked on, that updates the context's state. In this case I'm trying to keep track of the selected elements by an array. However, I can't seem to reference selectedTiles in the onClick function. It either won't compile, crashes on runtime, or doesn't do anything.
To be clear, if I remove the code lines relating to selectedTiles, it works and toggles the class just fine. I am also binding the function already in the constructor.
Here's the code I have right now, in the three relevant files.
index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import UserInterface from './components/userinterface';
import DisplayGrid from './components/displaygrid';
import {GridContextProvider} from './gridcontext';
ReactDOM.render(
<React.StrictMode>
<GridContextProvider>
<UserInterface />
<div className="grid-wrapper">
<DisplayGrid />
</div>
</GridContextProvider>
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
displaygrid.js
import React from 'react';
import {GridContext} from '../gridcontext'
class DisplayGrid extends React.Component{
static contextType = GridContext;
render(){
const gridData = this.context;
return (
<div>
{gridData.name}<br />
<div className= "grid-container" style={{gridTemplateColumns: gridData.columns}}>
{gridData.tiles}
</div>
</div>
);
}
}
export default DisplayGrid;
gridcontext.js
import React from "react";
const GridContext = React.createContext();
class GridContextProvider extends React.Component {
constructor(props) {
super(props);
const topGridData = JSON.parse(localStorage.getItem('grids'))[0];
this.state = this.makeGrid(topGridData.dims[0], topGridData.dims[1], topGridData.dims[2],
topGridData.visible, topGridData.hex);
this.selectTile = this.selectTile.bind(this);
this.makeGrid = this.makeGrid.bind(this);
}
selectTile(event) {
let tilesArray = this.state.selectedTiles;
if(event.currentTarget.className === "grid-tile"){
event.currentTarget.className = "grid-tileb";
tilesArray.push(event.currentTarget.key);
} else {
event.currentTarget.className = "grid-tile";
tilesArray.splice(tilesArray.indexOf(event.currentTarget.key),tilesArray.indexOf(event.currentTarget.key));
}
}
makeGrid(x, y, tilesize, visible, hex){
let columnStr = "";
let tileArray = [];
const widthStr = tilesize.toString() + "px"
for (let i = 0; i < y; i++) {
for (let j = 0; j < x; j++) {
if(i===0) columnStr = columnStr + "auto ";//x loops over columns so this runs once for all columns.
let div = (
<div
key={"x" + j.toString() + "y" + i.toString()}//for example at coordinates 5,6 id is x5y6. starts at 0.
className="grid-tile"
style={{
width: widthStr,
height: widthStr,
border: "1px solid rgba(0, 0, 0," + (visible ? "0.6)" : "0.0)")
}}
onClick={this.selectTile}
>
</div>
)
tileArray.push(div);
}
}
const gridsDataArray = JSON.parse(localStorage.getItem('grids'));
const index = JSON.parse(localStorage.getItem('currentGrid'));
return {
columns: columnStr,
tiles: tileArray,
selectedTiles: [],
name: gridsDataArray[index].name,
bgurl: gridsDataArray[index].bgurl
};
}
render() {
return (
<GridContext.Provider value={{
columns: this.state.columns,
tiles: this.state.tiles,
selectedTiles: this.state.selectedTiles,
name: this.state.name,
bgurl: this.state.bgurl,
setNameBgurl: (newName, newBgurl) => {
this.setState({name : newName,
bgurl : newBgurl});
},
setGrid: (x, y, tilesize, visible, hex) => {
this.setState(this.makeGrid(x, y, tilesize, visible, hex));
}
}}>
{this.props.children}
</GridContext.Provider>
);
}
}
export { GridContext, GridContextProvider };
This code gives me a "TypeError: this is undefined" crash on runtime when I click on a tile, pointing at line 15 of gridcontext, which is let tilesArray = this.state.selectedTiles;
which is strange since the method is bound.
Other feedback would be welcome since I'm new to React and not much better with javascript overall; I don't think I've quite wrapped my head around how javascript uses "this". I've been mostly trying to look up tutorials but it seems there are a lot of different ways to use React so it's hard to tell what practices are standard or not.