Solution 1 :

You may keep track of randomly selected cell (e.g. as an array of [x,y] indexes) then update your state (upon timer ticking or button click) so you get your matrix re-rendered:

const { useState } = React,
      { render } = ReactDOM,
      rootNode = document.getElementById('root')
      
const App = () => {
  const randomXY = () => [0|Math.random()*5, 0|Math.random()*5],
        [cell, setCell] = useState(randomXY()),
        nextCell = () => setCell(randomXY())
  return (
    <div>
    <div className="wrapper">
      {
        [...Array(5)]
          .map((_,i) => (
            <div className="row">
              {
                [...Array(5)]
                  .map((_,j) => (
                    <div 
                      className={`cell${i == cell[0] && j == cell[1] ? ' blue' : ''}`}
                    />
                  ))
               }
            </div>
          ))
      }
     </div>
     <button onClick={nextCell}>Reset</button>
     </div>
  )
  
}  

render (
  <App />,
  rootNode
)
.wrapper {
  display: flex;
  flex-direction: column;
}

.row {
  display: flex;
  flex-direction: row;
}

.cell {
  width: 20px;
  height: 20px;
  border: 1px solid black;
}

.blue {
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><div id="root"></div>

Problem :

I have a table with size 5×5.
I need to randomly change the color in one of the cells to blue.

What the best practice way to do it in ReactJS?

An example of how it should look in pure JS:
https://codesandbox.io/s/summer-brook-by835?file=/src/index.js

Comments

Comment posted by YourBuddy

Wow, very elegant solution! Brilliantly!

By