问题导致变量在React重新租户上重新评估

发布于 2025-01-19 09:54:01 字数 969 浏览 0 评论 0原文

我发现一个小问题导致实例化变量在反应功能组件中被错误地修改。

预期行为

I具有一个变量(对象),该变量(对象)在反应功能组件中通过这样的后续逻辑进行了修改:

const SomeComponent = () => {
  let foo = {}
  if (someCondition) {
     delete foo[prop]
  }
  if (someOtherCondition) {
     delete foo[anotherProp]
  }
  ...
  return (<> Object.keys(foo).map(key => {...})</>)
}

因此,基本上,我试图根据其他条件在此对象上仅使用特定属性,例如特定条件使用组件。

foo变量是从查询返回到数据库的对象。具体来说,在someComponent的父组件中,我正在运行一个查询,该查询(以对象的形式)返回有关公司的信息。 foo是该返回的一个属性,也是对象。查询完成后,查询的结果将传递到someconent中,因此foosomeComponent中不明显。

我注意到的问题

是,当上述组件第一次将上述组件带入DOM时,它可以按预期工作。因此,例如,如果我在“情况1”下将组件带入DOM,它将正常工作。但是,如果然后在另一种情况下将其带入DOM(我们称其为“情况2” - foo对象应具有不同集Prop 在“情况1”下渲染时,foo将无法正确修改

。 >从中删除的属性就好像somecondition

strong

I'm finding a small issue causing let instantiated variables to be modified incorrectly within React functional components.

Expected Behavior

I have a variable (an object) that is modified by follow-on logic like this inside a React functional component:

const SomeComponent = () => {
  let foo = {}
  if (someCondition) {
     delete foo[prop]
  }
  if (someOtherCondition) {
     delete foo[anotherProp]
  }
  ...
  return (<> Object.keys(foo).map(key => {...})</>)
}

So basically, I'm trying to use only specific properties on this object based on other conditions, like a specific conditional use of the Component.

The foo variable is an object that's returned from a query to a database. Specifically, in the parent Component to SomeComponent, I'm running a query that returns information (in the form of an object) about a company. foo is one property from that return that is also an object. When the query is complete, the result of the query is passed down into SomeComponent, so foo is not stateful in SomeComponent.

The Problem

I'm noticing that, when the above Component is brought into the DOM the first time, it works as expected. So, for example, if I bring the Component into the DOM under "circumstance 1", it will work properly. However, if I then bring it into the DOM under another, different circumstance (let's call it "circumstance 2"--a circumstance when the foo object should have a different set of props deleted from it that when rendered under "circumstance 1", foo will not be modified correctly.

Specifically, I notice that foo will have both the properties deleted from it as if it met both someCondition and someOtherCondition.

If you have any leads on this challenge, I'd appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文