问题导致变量在React重新租户上重新评估
我发现一个小问题导致让
实例化变量在反应功能组件中被错误地修改。
预期行为
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
中,因此foo
在someComponent
中不明显。
我注意到的问题
是,当上述组件第一次将上述组件带入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 prop
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论