I made a chess game using a React frontend. Originally, any moves were implemented seemingly instantaneously but after adding extra features like changing the background of the Header to reflect whose turn it is (white or black background depending on if its white's move) and other similar features, my app has noticeably slowed down. I suspect it's these types of if statements-
const Details = props => {
console.log(props.status);
let [backGround, setBackGround] = useState("w details")
const history = useHistory();
if (props.status.white && backGround === "bl details"){
setBackGround("w details")
} else if (!props.status.white && backGround === "w details"){
setBackGround("bl details")
}
that are responsible because the console.log printing the props will print its statement 8 times per move(originally it was twice)
I am using functional components rather than classes and my research on optimization led me to the following solutions-
React Hooks - How do I implement shouldComponentUpdate?
Should I wrap all my components with React.memo() if it is not expecting any props?
How to use shouldComponentUpdate with React Hooks?
all pointing to simple cases of React.memo but I am obviously missing something because when I attempt to implement it, all I get are a plethora of 'props' is not defined errors (one for each time I use props which is a lot).
Details.jsx
import React, { useState } from 'react';
import "../App.css"
import DataService from '../service/DataService';
import { useHistory } from 'react-router-dom';
let [backGround, setBackGround] = useState("w details")
const Details = React.memo(props => {if (props.status.white){setBackGround("w details")} else {setBackGround("bl details")}}, (prevProps, props) => prevProps.white === props.white); {
console.log(props.status);
const history = useHistory();
const undo = () => {
DataService.undo()
.then(res => {
console.log(res);
props.setTheBoard(res.data);
props.changeTurn();
})
.catch(err => {
console.log(err);
window.alert(err.response.data.errMessage)
})
}
const restart = () => {
DataService.restartGame()
.then(res => {
console.log(res);
props.setTheBoard(res.data);
props.changeTurn(true);
})
.catch(err => {
console.log(err);
window.alert(err.response.data.errMessage)
})
}
const newGame = () => {
history.push('/');
}
return (
<div className={backGround} >
{props.status.active ? <h2>It is {props.status.playerName}'s turn</h2> :
<div>
<h1 className="check">Game Over!</h1>
<button className="tooltip" onClick={restart}>RESTART<span className="tooltiptext">Play another game vs the same opponent</span></button>
<button className="tooltip" onClick={newGame}>NEW GAME<span className="tooltiptext">Play a game vs a different opponent</span></button>
</div>}
{props.status.active &&
<div>
{props.isMove ? <button className="detailButtons" onClick={props.specialMove}>Special Move</button> : <button className="detailButtons" onClick={() => props.endTheGame(true)}>Forfeit</button> }
{props.isMove ? <button className="detailButtons" onClick={props.unselect}>Unselect Piece</button> : <button className="detailButtons" onClick={() => props.endTheGame(false)}>Draw</button> }
{props.isMove ? <button className="detailButtons">Toggle Sidebar</button> : props.undo && <button className="detailButtons" onClick={() => undo()}>Undo</button> }
{props.status.check && <h1 className="check">You must move out of check!</h1>}
</div> }
</div>
);
}
export default Details;
Since the props in this component only changes when the turn changes (props.status.white) I thought this would be a good place to try to cut down on the unnecessary re-renders but all the solutions I see for it are very simple. Is it not possible to use React.memo when there is a widespread use of props like this?
How do I optimize the performance while still keeping access to props?