无法推送具有useRef作为其中一个元素的对象。

huangapple go评论65阅读模式
英文:

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: &quot;3.1&quot;,
      title: &quot;Review Methodology&quot;,
      ref: useRef(null),
      component: &lt;Methodology handleAddMethodology={() =&gt; {}} /&gt;,
    },
  ]);

on click of a new button I want to append an similar object into the previous state, the ideal output should look like -

[
    {
      index: &quot;3.1&quot;,
      title: &quot;Review Methodology&quot;,
      ref: useRef(null),
      component: &lt;Methodology handleAddMethodology={() =&gt; handleNewMethodology()} /&gt;,
    },
    {
      index: &quot;3.2&quot;,
      title: &quot;Review Methodology&quot;,
      ref: useRef(null),
      component: &lt;Methodology handleAddMethodology={() =&gt; handleNewMethodology()} /&gt;,
    }
]

I tried this but it did not work as we can't use useRef inside a callback

  const handleNewMethodology = () =&gt; {
    const newMethodology = {
      index: `3.${methodology.length + 1}`,
      title: &quot;New Methodology&quot;,
      ref: useRef(null),
      component: &lt;Methodology handleAddMethodology={HandleAddMethodology} /&gt;,
    };
    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 &#39;react&#39;;
import Methodology from &#39;./Methodology&#39;;

function MyComponent() {
  const [methodology, setMethodology] = useState([
    {
      index: &quot;3.1&quot;,
      title: &quot;Review Methodology&quot;,
      ref: useRef(null),
      component: &lt;Methodology handleAddMethodology={handleNewMethodology} /&gt;,
    },
  ]);

  const newMethodologyRef = useRef(null);

  const handleNewMethodology = () =&gt; {
    const newMethodology = {
      index: `3.${methodology.length + 1}`,
      title: &quot;New Methodology&quot;,
      ref: newMethodologyRef,
      component: &lt;Methodology handleAddMethodology={handleNewMethodology} /&gt;,
    };
    setMethodology(prevMethodology =&gt; [...prevMethodology, newMethodology]);
  };

  // This effect is needed to initialize newMethodologyRef for each new instance
  useEffect(() =&gt; {
    newMethodologyRef.current = null;
  }, [methodology]);

  // ... Rest of your component logic
}

huangapple
  • 本文由 发表于 2023年6月22日 20:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定