将 React 对象设置为 props 中给定的对象
我试图根据传递到组件中的 props (props.toothData
) 来设置 React 中对象 (teethData.data
) 的状态。如果传递的 props 对象的长度为零,则它会设置。如果 props.toothData
长度为零,则为变量 givenToothData
赋予默认值,以便 teethData.data
具有默认值,否则,< code>teethData.data 只是设置为 props.toothData
。但是,如果 props.ToothData
长度不为零,则 teethData.data
也不会改变我想要的方式
下面是我的代码,有什么建议吗?
function TableArray(props) {
const navigate = useNavigate();
console.log(props.toothData);
var givenToothData = props.toothData;
if (Object.keys(props.toothData).length === 0) {
console.log("this happened", props.toothData);
givenToothData = {
1: {
pp: false,
C14: false,
GI0: false,
GI23: false,
PDI: false,
GR: false,
F13: false,
M03: false,
},
2: {
AB: false,
AT: false,
EH: false,
TI: false,
TLUX: false,
O: false,
GH: false,
OST: false,
ONF: false,
FX: false,
CA: false,
OM: false,
D: false,
DX: false,
},
3: { RTR: false, TR: false, SN: false, TA: false },
4: {
TFXUCF: false,
TFXCCF: false,
TFXUCRF: false,
TNE: false,
TPE: false,
},
}
console.log("this happened", givenToothData);
}
const [teethData, setTeethData] = useState({
tooth: props.tooth,
data: givenToothData
});
console.log(teethData.data, "runthisfirst \n", givenToothData, props.teethData);
I am trying to set the state of of a an object (teethData.data
) in react based on the props passed into the component (props.toothData
). if the props passed object has length zero then it sets. If props.toothData
is length zero then a default value is given to a variable givenToothData
so that teethData.data
has a default value otherwise, teethData.data
is just set to props.toothData
. However teethData.data
doesn't change the way i want it too if props.ToothData
isn't length zero
Below is my code, is there any advice?
function TableArray(props) {
const navigate = useNavigate();
console.log(props.toothData);
var givenToothData = props.toothData;
if (Object.keys(props.toothData).length === 0) {
console.log("this happened", props.toothData);
givenToothData = {
1: {
pp: false,
C14: false,
GI0: false,
GI23: false,
PDI: false,
GR: false,
F13: false,
M03: false,
},
2: {
AB: false,
AT: false,
EH: false,
TI: false,
TLUX: false,
O: false,
GH: false,
OST: false,
ONF: false,
FX: false,
CA: false,
OM: false,
D: false,
DX: false,
},
3: { RTR: false, TR: false, SN: false, TA: false },
4: {
TFXUCF: false,
TFXCCF: false,
TFXUCRF: false,
TNE: false,
TPE: false,
},
}
console.log("this happened", givenToothData);
}
const [teethData, setTeethData] = useState({
tooth: props.tooth,
data: givenToothData
});
console.log(teethData.data, "runthisfirst \n", givenToothData, props.teethData);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
useState 钩子的参数仅在组件首次安装时分配一次。在后续函数执行时,它不会设置该值。每当你的 props 改变时,尝试使用 useEffect 设置状态。即,
useEffect(() => { // 更新状态的逻辑 },[props.toothData])
。Argument of useState hook is assigned only once when your component first mounted. On the subsequent function execution it won't set that value. Try using useEffect to set the state whenever your props change. i.e.,
useEffect(() => { // your logic to update state },[props.toothData])
.