Показать сообщение отдельно
  #17 (permalink)  
Старый 25.11.2019, 13:28
Аватар для SuperZen
Профессор
Отправить личное сообщение для SuperZen Посмотреть профиль Найти все сообщения от SuperZen
 
Регистрация: 08.11.2017
Сообщений: 642

import React from 'react'
import './App.css'

const CELL_COUNT = 25

function App() {
  const [cells] = React.useState(Array.from({ length: CELL_COUNT }, (_, i) => i))
  const [randomCell, setRandomCell] = React.useState(null)
  const playClick = () => setRandomCell(parseInt(Math.random() * CELL_COUNT))
  const greenClick = () => setRandomCell(null)

  return (
    <div className="game">
      <div>
        <button onClick={playClick}>Play</button>
      </div>
      <div>
        {cells.map((cell, idx) => <button
          key={cell}
          className={idx === randomCell ? 'cell random' : 'cell'}
          onClick={randomCell && greenClick}
        >{cell}</button>)}
      </div>
    </div>
  )
}

export default App
Ответить с цитированием