简体   繁体   English

Function 仅被传递给 React 子组件一次

[英]Function only being passed to React child component once

I've got something I don't understand here.我有一些我不明白的地方。 I'm creating a hierarchy of React functional components like so:我正在创建一个 React 功能组件的层次结构,如下所示:

const ContainerComponent = () => {
  const [total, setTotal] = useState(initialValue);
  const updateFunction = () => { console.log('It got called'); }

  return (
    <EntryComponent updateFunction={updateFunction} />
  );
}

const EntryComponent = ({updateFunction}) => {
  const services = [{}, {}, {}];

  return (
    {services.map((service) => <ServiceComponent updateFunction={updateFunction} />}
  );
}

const ServiceComponent = ({updateFunction}) => {
  return (
    <input type='checkbox' onChange={updateFunction} ></input>
  );
}

So the idea is that the function gets passed all the way to the ServiceComponent and attached to the checkbox for each component.所以想法是 function 一直传递到ServiceComponent并附加到每个组件的复选框。 On change the function should fire, running the code and updating the total value in the ContainerComponent .在更改 function 应该触发,运行代码并更新ContainerComponent中的total Problem is that this isn't happening properly and the function seems only to be passed to the first ServiceComponent instance.问题是这没有正确发生,并且 function 似乎只传递给第一个ServiceComponent实例。 If I drop a console.log(updateFunction) into ServiceComponent I see the function logged for the first component, and undefined for the remaining two.如果我将console.log(updateFunction)放到ServiceComponent中,我会看到 function 为第一个组件记录,其余两个undefined This is strange even for JavaScript.即使对于 JavaScript 来说,这也很奇怪。 Can anyone shed any light as to what is going on?任何人都可以阐明发生了什么吗? As far as I understand the function should be able to be passed like any other variable, and each ServiceComponent should have it to use when needed.据我了解 function 应该能够像任何其他变量一样被传递,并且每个ServiceComponent都应该在需要时使用它。 Indeed, if I pass a second property to the child, an object or a primitive like an integer it comes through just fine on every child component.实际上,如果我将第二个属性传递给子组件,即 object 或像 integer 这样的原语,那么它在每个子组件上都可以通过。 Why is this not occurring for my function, and how do I fix it?为什么我的 function 没有出现这种情况,我该如何解决?

Edit: I see the problem, and the problem is I'm not as smart as I think I am.编辑:我看到了问题,问题是我没有我想的那么聪明。 In the condensed version I put here everything is being supplied to the child components in the same loop, but in the actual project the child components are created in multiple places and I neglected to pass the function property to all of them.在精简版中,我将所有内容都提供给同一个循环中的子组件,但在实际项目中,子组件是在多个位置创建的,我忽略了将 function 属性传递给它们。 Which is mind mindbogglingly stupid on my part.就我而言,这真是令人难以置信的愚蠢。 I'm leaving the question here as many of you have posted replies, for which I'm grateful.我把这个问题留在这里,因为你们中的许多人已经发布了回复,对此我很感激。

Programming is hard, I think I need a better brain.编程很难,我想我需要一个更好的大脑。

I tried refactoring your code to this我尝试将您的代码重构为此

const ContainerComponent = () => {
  const [total, setTotal] = useState(0);
  const updateFunction = (e) => {
    console.log("update total stuff happens here");
    console.log(e);
  };

  return <EntryComponent updateFunction={updateFunction} />;
};

const EntryComponent = ({ updateFunction }) => {
  const services = ["one", "two", "three"];

  return (
    <>
      {services.map((service) => (
        <ServiceComponent updateFunction={updateFunction} />
      ))}
    </>
  );
};

const ServiceComponent = ({ updateFunction }) => (
  <input type="checkbox" onChange={updateFunction}></input>
);

and it works just fine.它工作得很好。 Try also using a react fragment in your entry component.尝试在你的入口组件中使用一个反应片段。

Edited after Author changed provided code在作者更改提供的代码后编辑

You've got completely valid code here.你在这里得到了完全有效的代码。 I suspect it's something to do with your passed function.我怀疑这与您通过的 function 有关。 Will check back as you provide more details.将在您提供更多详细信息时回来查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM