【发布时间】:2022-01-05 18:21:24
【问题描述】:
点击一组难度按钮后,我想在“/game”路由中渲染一个 Tabel 组件。因此,应用程序必须根据在 DifficultyButtons.js 中设置的难度状态从路由“/”传递到“/game”,从而呈现适当的表格。 我在 App.js 中管理 Route 组件,如下所示:`
<Route path='/' render={() => <DifficultyButtons/>} />
<Route path='/game' render={() => <Table/>} />
`
在文件 DifficultyButtons.js 中,重定向从单击设置状态“难度”的按钮开始,该状态将在文件 Table.js 中使用,我的目的是根据设置的难度级别按比例呈现表格。这是 Table.js:
`
const location = useLocation();
const rows = N*location.state;
const columns = M*location.state;
return (
<Table responsive>
<tbody>
{Array.from({length: rows}).map((_, row_index) => (
<tr key={row_index}>
{Array.from({length: columns}).map((_, column_index) => (
<td key={column_index}>
try
</td>
))}
</tr>
))}
</tbody>
</Table>
</div>
)
` 这是呈现比例表的正确方法吗?我想了解为什么重定向到“/game”不起作用。 DifficultButtons.js 中的重定向代码:
{selected ? (
<Redirect to={{
pathname: '/game',
state: {level:difficulty}
}}
【问题讨论】:
标签: javascript reactjs react-hooks react-router react-table