未能第二次更新变量值
我想在单击按钮时将数组值从较高组件移至较低的组件。
在较低的组件中,我得到这样的道具:
useEffect(() => {
if (props.poolInfo) {
setPoolInfo(props.poolInfo)
diskSlot.push(props.poolInfo.disks_enc_slot)
const firstِDiskSlot = Object.freeze(diskSlot);
console.log(firstِDiskSlot );//[58:0,58:1] For the first time rendreing
checkAllDiskInDriveGroup(props.poolInfo.disks_enc_slot, driveData)
}
return () => {
setPoolInfo(false)
setFormData({
count: {
options: [],
selectedValue: ""
},
disk: {
options: [],
selectedValue: ""
},
})
}
}, [props.poolInfo])
第二次渲染并为更高的组件添加值:
console.log(firstِDiskSlot); //[58:0,58:1,58:3:58:4]
但是我想(第一个ِ磁盘)呈现的第一个值应该保留而不要更改,而不是因为我需要第一个值,因为我需要第一个值:[[[[[[[ 58:0,58:1]
如何冻结不变第二次变化的初始值(第一个disklot)?
I want to move an array value from a higher component to a lower order component with the click of a button.
In the lower order component, I get props like this:
useEffect(() => {
if (props.poolInfo) {
setPoolInfo(props.poolInfo)
diskSlot.push(props.poolInfo.disks_enc_slot)
const firstِDiskSlot = Object.freeze(diskSlot);
console.log(firstِDiskSlot );//[58:0,58:1] For the first time rendreing
checkAllDiskInDriveGroup(props.poolInfo.disks_enc_slot, driveData)
}
return () => {
setPoolInfo(false)
setFormData({
count: {
options: [],
selectedValue: ""
},
disk: {
options: [],
selectedValue: ""
},
})
}
}, [props.poolInfo])
Rendering a second time and adding value to the higher component:
console.log(firstِDiskSlot); //[58:0,58:1,58:3:58:4]
But I want to (firstِDiskSlot) The first value that is rendered should remain and not change because I need the first value:[58:0,58:1]
How do I freeze the initial value (firstِDiskSlot) that does not change a second time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想运行一次(第一个Prop值),则只需删除依赖项并留下空数组 -
useffect(()=> ...,[])
。如果问题是第一个值可能是空数组或未定义的,则在Useref中引入稳定的常数并在使用效果中填充它,但只有以前没有填充,并且像这样,您将在参考中只有第一个有效的值。If you want to run it only once(on first prop value) then just remove dependency and leave empty array -
useEffect(() => ..., [])
. If the problem is that first value might be the empty array, or undefined, then introduce stable constant with useRef and fill it in useEffect, but only if not previously filled, and like that you will have only first valid value in reference.