javascript - 为什么撤消变量没有更新?
问题描述
这是我的代码:
import useUndo from 'use-undo';
export default function UndoTable(props){
const [hightLightCellIndex, setHightLightCellIndex] = useState(-1);
let [
rosterList,
undoUtil
]=useUndo(props.rosterTableData.rosterList);
let activeShiftInfoList=props.rosterTableData.shiftInfoList;
let calendarUtility=props.rosterTableData.calendarUtility;
let monthlyCalendar=props.rosterTableData.monthlyCalendar;
let contextValue={
activeShiftInfoList,
calendarUtility,
hightLightCellIndex,
monthlyCalendar,
rosterList,
setHightLightCellIndex,
undoUtil
};
console.log(Object.keys(props.rosterTableData.rosterList["ITO1_1999-01-01"].shiftList).length);
console.log(Object.keys(rosterList.present["ITO1_1999-01-01"].shiftList).length);
//console.log(contextValue);
return (
<table>
</table>
)
}
我正在使用use-undo
包来实现撤消功能。
我使用 useUndo 挂钩将父提供的数据(即props.rosterTableData.rosterList
)转换为撤消变量rosterList
。
问题:
我发现rosterList
更新内容时,内容不会props.rosterTableData.rosterList
更新。
我试过的:
我试图将代码修改为以下内容:
import useUndo from 'use-undo';
export default function UndoTable(props){
const [hightLightCellIndex, setHightLightCellIndex] = useState(-1);
let [
rosterList,
undoUtil
]=useUndo(props.rosterTableData.rosterList);
useEffect(()=>{
console.log(Object.keys(props.rosterTableData.rosterList["ITO1_1999-01-01"].shiftList).length);
console.log(Object.keys(rosterList.present["ITO1_1999-01-01"].shiftList).length);
},[props.rosterTableData])
//console.log(contextValue);
return (
<table>
</table>
)
}
不幸的是,问题仍然存在。有没有办法解决这个问题?
解决方案
您似乎使用useUndo
不正确。查看文档
您传递给useUndo
函数的是initialState
,因此如果您传递不同的值,它将不会更新。如果你想重置撤销状态,你应该调用 reset 方法:
useEffect(() => {
undoUtil.reset(props.rosterTableData.rosterList)
}, [props.rosterTableData.rosterList])
推荐阅读
- spring-cloud-sleuth - Spring JMS 消费者没有正确设置父跨度 ID
- java - Android - 整个数据没有存储在 Firebase 数据库中
- android - 为什么这个 onClickListener 会给出 NullPointerException?
- android - onMenuItemClick 切换案例选择多个案例
- pandas - 尽管数据类型相同,熊猫合并返回空帧?
- swift - 无法安装 Cocoapods,不断出现错误
- python - 自动将新行上的所有值添加在一起,并在输入字段中显示它们
- javascript - 如何使用 Selenium 和 JavaScript 检查类中是否存在文本
- javascript - 延迟加载和砌体
- javascript - 遍历数组中的对象并根据现有属性的正则表达式匹配添加新属性