英文:
Unable to push an object having useRef as one of the elements
问题
以下是我的状态对象,我使用它来渲染一个垂直进度表单:
const [methodology, setMethodology] = useState([
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => {}} />,
},
]);
点击一个新按钮后,我想将一个类似的对象附加到先前的状态,理想的输出应如下所示:
[
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => handleNewMethodology()} />,
},
{
index: "3.2",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => handleNewMethodology()} />,
}
]
我尝试了这个,但它没有工作,因为我们不能在回调函数内部使用 useRef:
const handleNewMethodology = () => {
const newMethodology = {
index: `3.${methodology.length + 1}`,
title: "New Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={HandleAddMethodology} />,
};
setMethodology([...methodology, newMethodology]);
};
什么是理想的解决方案?
英文:
Following is my state object using which I render a form which is a vertical progress form:
const [methodology, setMethodology] = useState([
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => {}} />,
},
]);
on click of a new button I want to append an similar object into the previous state, the ideal output should look like -
[
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => handleNewMethodology()} />,
},
{
index: "3.2",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={() => handleNewMethodology()} />,
}
]
I tried this but it did not work as we can't use useRef inside a callback
const handleNewMethodology = () => {
const newMethodology = {
index: `3.${methodology.length + 1}`,
title: "New Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={HandleAddMethodology} />,
};
setMethodology([...methodology, newMethodology]);
};
What would be the ideal solution?
答案1
得分: 1
在React中,useRef
是一个钩子,它应该只在React组件的顶层调用。您在指出我们不能在回调函数中像handleNewMethodology
中那样使用useRef
是正确的。
相反,我们可以在组件的顶层创建新的ref,然后在更新状态时使用它。尝试这个 ->
import { useState, useRef, useEffect } from 'react';
import Methodology from './Methodology';
function MyComponent() {
const [methodology, setMethodology] = useState([
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={handleNewMethodology} />,
},
]);
const newMethodologyRef = useRef(null);
const handleNewMethodology = () => {
const newMethodology = {
index: `3.${methodology.length + 1}`,
title: "New Methodology",
ref: newMethodologyRef,
component: <Methodology handleAddMethodology={handleNewMethodology} />,
};
setMethodology(prevMethodology => [...prevMethodology, newMethodology]);
};
// 这个效果是为了为每个新实例初始化newMethodologyRef
useEffect(() => {
newMethodologyRef.current = null;
}, [methodology]);
// ... 其余的组件逻辑
}
英文:
In React, useRef
is a hook, and it should only be called at the top level of your React components. You're correct in stating that we can't use useRef
inside a callback function like handleNewMethodology
.
Instead, we can create the new ref at the top level of our component and then use it when we update our state. Try this out -->
import { useState, useRef, useEffect } from 'react';
import Methodology from './Methodology';
function MyComponent() {
const [methodology, setMethodology] = useState([
{
index: "3.1",
title: "Review Methodology",
ref: useRef(null),
component: <Methodology handleAddMethodology={handleNewMethodology} />,
},
]);
const newMethodologyRef = useRef(null);
const handleNewMethodology = () => {
const newMethodology = {
index: `3.${methodology.length + 1}`,
title: "New Methodology",
ref: newMethodologyRef,
component: <Methodology handleAddMethodology={handleNewMethodology} />,
};
setMethodology(prevMethodology => [...prevMethodology, newMethodology]);
};
// This effect is needed to initialize newMethodologyRef for each new instance
useEffect(() => {
newMethodologyRef.current = null;
}, [methodology]);
// ... Rest of your component logic
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论