reactjs - 文本字段在表格 ReactJs 中无法正常工作
问题描述
我有一个包含对象数组的对象
initialPreconfigTodoState = {
todos: [
{
title: 'title1',
dueDate: new Date(),
create: true,
assignedTo: 'role',
},
{
title: 'title2',
dueDate: new Date(),
create: true,
assignedTo: 'role',
}]
};
我使用这个对象数组来控制表格中文本字段的状态
const [preconfig, setPreconfig] = useState(initialPreconfigTodoState);
{preconfig.todos.map((todo, index) => {
return (
<TableRow
className="h-64 cursor-pointer"
key={Math.random()}
>
<TableCell className="th" component="th" scope="row">
<TextField
id={`titleForm${index}`}
name={`titleForm${index}`}
onChange={(event) => handleTitle(event, index)}
value={todo.title}
type="text"
variant="outlined"
required={todo.create ? true : false}
fullWidth
/>
</TableCell>
当我尝试输入文本字段时,我不知道会发生什么,但我需要为我输入的每个字符单击文本字段,我认为是 useState 钩子在重新渲染组件时导致此问题,我不能找到解决方案。
这是我在 onChange 回调上的句柄函数,
const handleTitle = (event, index) => {
let newArray = [...preconfig.todos];
newArray[index] = {
...newArray[index],
title: event.target.value,
};
setPreconfig({ todos: newArray });
};
这是完整的代码
const initialPreconfigTodoState = {
todos: [
{
title: "title1",
dueDate: new Date(),
create: true,
assignedTo: "Role"
},
{
title: "title2",
dueDate: new Date(),
create: true,
assignedTo: "Role"
}
]
};
function TodoDialog() {
const [preconfig, setPreconfig] =
useState(initialPreconfigTodoState);
const handleTitle = (event, index) => {
let newArray = [...preconfig.todos];
newArray[index] = {
...newArray[index],
title: event.target.value
};
setPreconfig({ todos: newArray });
};
return (
<div>
<Dialog open={true} maxWidth="xl" scroll="paper">
<DialogContent>
<div>
<Table aria-labelledby="tableTitle">
<TableBody>
<TableRow className="h-64 cursor-pointer" key=.
{Math.random()}>
<TableCell className="th" component="th" scope="row">
Title
</TableCell>
<TableCell className="th" component="th" scope="row">
Due Date
</TableCell>
<TableCell className="th" component="th" scope="row">
Asigned To
</TableCell>
<TableCell className="th" component="th" scope="row">
Create
</TableCell>
</TableRow>
{preconfig.todos.map((todo, index) => {
return (
<TableRow
className="h-64 cursor-pointer"
key={Math.random()}
>
<TableCell className="th" component="th" scope="row">
<TextField
id={`titleForm${index}`}
name={`titleForm${index}`}
onChange={(event) => handleTitle(event, index)}
value={todo.title}
type="text"
variant="outlined"
required={todo.create ? true : false}
fullWidth
/>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
</DialogContent>
<DialogActions>
<Button color="primary" variant="contained">
Cancel
</Button>
<Button type="submit" color="primary" variant="contained" autoFocus>
Save
</Button>
</DialogActions>
</Dialog>
</div>
);
}
解决方案
那是因为key={Math.random()}
在 TableRow 上。
将其更改为key={index}
,它应该可以工作。
随着Math.random()
您的键更改每个渲染和反应失去它的参考。
推荐阅读
- mysql - 如何让数据库/sql 并行查询多个模式?
- python - 在 Mac 上,应该将 PYTHONPATH 设置为什么才能使用“python manage.py run_command”?
- ios - Fastlane 扫描返回错误 No test bundle product for testingSpecifier
- python-3.x - 如何使用python和opencv,numpy将像素值数组转换为图像以显示
- c# - 使用 iPad Pro 自动化设置面板
- python - MySql 5.7 错误(这与 sql_mode=only_full_group_by 不兼容)
- java - 方法调用“toString”可能会产生“java.lang.NullPointxception”
- powershell - 根据值从数组中选择一组值
- c - C程序不会按升序排列数字
- java - 如何使用 Java 设置 JLabel 的颜色?