当我将数据传递给一个组件时,我得到了未定义的结果。

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

when I pass data to a component I get undefined

问题

好的,以下是您要翻译的内容:

"嗯,我在一个Next.js项目中有一个上下文(Context),在那里我处理模态框(modals),我将模态框的详细信息存储在数组(modalBase)中。同时,我从另一个上下文(toolsContext)中获取数据并将其传递给组件。但是,当我在这个上下文中使用console.log输出数据时,它会显示数据,但在将数据传递给组件后,组件中却显示为undefined。我感到困惑。

  const [data, setData] = useState();
  const [modals, setModals] = useState([]);

  const router = useRouter();
  const query = router.query;

  const { collectionData } = useContext(toolsContext);

  useEffect(() => {
    const getData = async () => {
      const ss = await collectionData(query.taskId);
      setData(ss);
    };
    getData();
  }, []);

  // ======= 模态框详情
  const modalsBase = [
    {
      name: "collectCreator",
      openStatus: false,
      content: <CollectionForm />,
    },
    {
      name: "taskItemCreator",
      openStatus: false,
      content: <TaskItemForm data={data} />,
    },
    {
      name: "taskCreator",
      openStatus: false,
      content: <TasksForm data={data} />,
    },
    {
      name: "collectionEdit",
      openStatus: false,
      content: <CollectionEditForm data={data} />,
    },
    {
      name: "taskItemEdit",
      openStatus: false,
      content: <TaskItemEditForm />,
    },
    {
      name: "tasksEdit",
      openStatus: false,
      content: <TasksEditForm />,
    },
  ];

我认为我遇到了这个问题是因为我在数组中有组件,并且我尝试将数据传递给这些组件。"

英文:

well,I have Context in nextjs project where I work on modals and I put modal details in the array(modalBase), also I get data from another context(toolsContext) into this context to pass data to components, but When I console data in this context it's given me data but in the components, after passing data to components it gives me undefined, I am confused

  const [data, setData] = useState();
  const [modals, setModals] = useState([]);

  const router = useRouter();
  const query = router.query;

  const { collectionData } = useContext(toolsContext);

  useEffect(() =&gt; {
    const getData = async () =&gt; {
      const ss = await collectionData(query.taskId);
      setData(ss);
    };
    getData();
  }, []);

  // ======= Modal details
  const modalsBase = [
    {
      name: &quot;collectCreator&quot;,
      openStatus: false,
      content: &lt;CollectionForm /&gt;,
    },
    {
      name: &quot;taskItemCreator&quot;,
      openStatus: false,
      content: &lt;TaskItemForm data={data} /&gt;,
    },
    {
      name: &quot;taskCreator&quot;,
      openStatus: false,
      content: &lt;TasksForm data={data} /&gt;,
    },
    {
      name: &quot;collectionEdit&quot;,
      openStatus: false,
      content: &lt;CollectionEditForm data={data} /&gt;,
    },
    {
      name: &quot;taskItemEdit&quot;,
      openStatus: false,
      content: &lt;TaskItemEditForm /&gt;,
    },
    {
      name: &quot;tasksEdit&quot;,
      openStatus: false,
      content: &lt;TasksEditForm /&gt;,
    },
  ];

I think that I get this problem because I have components in the array and I try to pass data to components

答案1

得分: 1

The data is undefined in your components because you're initializing it asynchronously in a useEffect hook, and then immediately using it in your modalsBase array. Remember that useEffect is executed after the component render, and fetching the data is also an asynchronous operation.

This means that when your modalsBase array is being defined, data is still undefined. It only gets a value after the data fetching has completed, and by this time, your modalsBase array has already been defined with undefined data.

To fix this issue, you should move your modalsBase array inside a useEffect hook that has data as a dependency. This way, the array will be redefined every time data changes. Here's how you can do it:

const [modals, setModals] = useState([]);

useEffect(() => {
  const modalsBase = [
    {
      name: "collectCreator",
      openStatus: false,
      content: <CollectionForm />,
    },
    {
      name: "taskItemCreator",
      openStatus: false,
      content: <TaskItemForm data={data} />,
    },
    // ...
  ];

  setModals(modalsBase);
}, [data]);

Now, modalsBase is redefined every time data changes, which includes the initial fetch. The components will receive the updated data correctly.

Also, I recommend initializing data as null instead of undefined to make it more explicit that it is intentionally empty at the start. That way, you can use conditionals to render different things based on whether data is null or has a value. You can do this by changing the line const [data, setData] = useState(); to const [data, setData] = useState(null);.

Remember that this will cause a re-render every time data changes. If data changes frequently, you might want to consider a different approach, such as passing a loading state to the components, or fetching the data inside the components themselves.

英文:

The data is undefined in your components because you're initializing it asynchronously in a useEffect hook, and then immediately using it in your modalsBase array. Remember that useEffect is executed after the component render, and fetching the data is also an asynchronous operation.

This means that when your modalsBase array is being defined, data is still undefined. It only gets a value after the data fetching has completed, and by this time, your modalsBase array has already been defined with undefined data.

To fix this issue, you should move your modalsBase array inside a useEffect hook that has data as a dependency. This way, the array will be redefined every time data changes. Here's how you can do it:

const [modals, setModals] = useState([]);

useEffect(() =&gt; {
  const modalsBase = [
    {
      name: &quot;collectCreator&quot;,
      openStatus: false,
      content: &lt;CollectionForm /&gt;,
    },
    {
      name: &quot;taskItemCreator&quot;,
      openStatus: false,
      content: &lt;TaskItemForm data={data} /&gt;,
    },
    // ...
  ];

  setModals(modalsBase);
}, [data]);

Now, modalsBase is redefined every time data changes, which includes the initial fetch. The components will receive the updated data correctly.

Also, I recommend initializing data as null instead of undefined to make it more explicit that it is intentionally empty at the start. That way, you can use conditionals to render different things based on whether data is null or has a value. You can do this by changing the line const [data, setData] = useState(); to const [data, setData] = useState(null);.

Remember that this will cause a re-render every time data changes. If data changes frequently, you might want to consider a different approach, such as passing a loading state to the components, or fetching the data inside the components themselves.

huangapple
  • 本文由 发表于 2023年7月20日 20:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729962.html
匿名

发表评论

匿名网友

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

确定