【发布时间】:2022-01-21 10:05:34
【问题描述】:
我尝试构建不同大小的 Tictactoe 游戏,并创建一个游戏菜单来选择要玩的棋盘。但是在使用单选表单选择电路板尺寸后,我对如何重定向或导航开始按钮感到困惑。我只构建两个板尺寸只是为了测试。这是我的代码
Menu.js:
import React, { Component,useState } from 'react'
import { useNavigate } from 'react-router-dom'
import Game from './Game'
import Game2 from './Game2'
export default function Menu() {
const [radio, setRadio] = useState();
// let navigate = useNavigate();
return (
<div>
<div className='title'>
<h1>Tic Tac Toe</h1>
</div>
<div className='board-options'>
<div>Choose Board Size: {radio}</div>
<div className="size-options">
<form>
<label>
<input type="radio"
name='size'
checked={radio === '3x3'}
value='3x3'
onChange={(e) => {setRadio(e.target.value)}} />
<img src="./images/grid-3.png" alt="grid-3" />
</label>
<label>
<input type="radio"
name='size'
checked={radio === '4x4'}
value='4x4'
onChange={(e) => {setRadio(e.target.value)}} />
<img src="./images/grid-4.png" alt="grid-4" />
</label>
<label>
<input type="radio"
name='size'
checked={radio === '5x5'}
value='5x5'
onChange={(e) => {setRadio(e.target.value)}} />
<img src="./images/grid-5.png" alt="grid-5" />
</label>
<label>
<input type="radio"
name='size'
checked={radio === '6x6'}
value='6x6'
onChange={(e) => {setRadio(e.target.value)}} />
<img src="./images/grid-6.png" alt="grid-6" />
</label>
</form>
<button type='submit' id='start'>START</button>
</div>
</div>
</div>
)
}
我尝试使用 react-router-dom 中的 useNavigate,但导致菜单功能未呈现。我希望使用开始按钮导航到我选择的游戏文件。
【问题讨论】:
-
你的App有没有实现路由切换?
-
不,实际上我没有尝试过,但是如果您在 react-router-dom 中引用
路由,他们将其删除并更改为 v6 上的 。你能详细说明你的建议吗?谢谢! @LahEzcen
标签: javascript reactjs react-hooks react-router tic-tac-toe