如何返回多次相同的组件但具有独立状态?
嗨,React中的大家,我想做类似的渲染范围函数中的组件2倍,
const Main()=>{
const [names, setNames] = useState([]);
const [selected, setSelected] = useState(-1);
return(
<Component1 name={names[selected].name}
setName={setNames}/>
<Component1 name={names[selected].name}
setName={setNames}/>
)
}
我通过setnames
从main()
到component1
setnames
/代码>处理状态 并从main()传递names
,然后在选择component1
<StyledSelect
ml="10px"
onChange={(e) => {
setName(e.target.value);
value={name}>
{names.map(({ name}) => {
return (
<option key={name} value={name}>
{name}
</option>
);
})}
我的组件中映射它的html select,其中包含名称,当我选择一个时,它触发了一个IT触发a IT IT在数据库中并检索名称数据(年龄,位置),问题是当我在第二个Component1上选择名称时,它还将选择相同的名称并在我的第一个组件上检索相同的名称数据,我希望它们独立。如何 ?
我试图放下那样的钥匙
return(
<Component1 key="compare" name={name}/>
<Component1 key="compared" name={name}/>
)
,但没有任何变化,
感谢
Hi guys in react i want to do something like render 2 times a component in same render function like this
const Main()=>{
const [names, setNames] = useState([]);
const [selected, setSelected] = useState(-1);
return(
<Component1 name={names[selected].name}
setName={setNames}/>
<Component1 name={names[selected].name}
setName={setNames}/>
)
}
This where i passed setNames
from Main()
to Component1
to handle state
and pass names
from Main() then map it in select of Component1
<StyledSelect
ml="10px"
onChange={(e) => {
setName(e.target.value);
value={name}>
{names.map(({ name}) => {
return (
<option key={name} value={name}>
{name}
</option>
);
})}
my component has an html select that has names in it, when i choose one it trigger a it in database and retrieve name data (age, location), the problem is when i select name on my second Component1, it will also select the same name and retrieve same name data on my first Component1, and i want them to be independent. How ?
I tried to put key like that
return(
<Component1 key="compare" name={name}/>
<Component1 key="compared" name={name}/>
)
but nothing changes
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题措辞错误(这与状态无关,与道具以及反应渲染方式无关)并且缺少信息,但是这里有一些可能会有所帮助的信息:
如果要使下拉列表的外观和/或功能不同,则
name
Prop将需要不同。目前,component1
的两个实例都得到了相同的name
属性,这就是为什么它们看起来和表现相同的原因。This question is worded incorrectly (this has nothing to do with state and everything to do with props and how React renders) and is missing information, but here are a few pieces of information that might help:
If you want to make the dropdowns different in appearance and/or functionality, the
name
prop will need to be different. Right now, both instances ofComponent1
are being given the samename
property, which is why they probably look and behave identically.(我想发表评论,但不能)
我不确定(我必须看到完整的代码),但我认为:
必须
这样的方式它们不共享相同的变量
(i would like can comment but i can't)
i am not sure (i must see the complete code) but i think that:
must be
this way they don't share the same variable
您在哪里打电话并设置名称?由于您将相同的道具名称传递给两个人,它们将是相同的。
要么在组件1本身内设置名称,要么为每个组件创建不同的状态。
Where is it that you're making the call and setting the name? Since you're passing the same prop name to both of them they will be the same.
Either you set name inside Component1 itself or you make a different state for each Component1.
我也有类似的情况,但就我而言,这是我需要使独特的ID。
我有一个组件,该组件在React Bootstrap的组中渲染了一个受控toggleButtons的列表。我想显示2个按钮列表,既包含在不同类别下可以选择的名称列表。最后,我必须设置ID而不是密钥。我最初将其设置为列表的索引。但是,当然,这两个列表都具有相同的索引,因此只有第一个具有状态并按下第二个列表将从第一个列表中选择或取消选择。为了解决它,我通过道具向下传递了一个ID,然后将ID附加到索引中以使其与众不同。
I had a similar situation but in my case it was an id that I needed to make unique.
I had component that rendered a list of controlled ToggleButtons in a group from React Bootstrap. I wanted to display 2 lists of the buttons, both containing the same list of names that could be selected under different categories. In the end I had to set the id NOT the key. I originally had set it as the index of the list.. but of course, both lists had identical indexes so only the first one had state and pressing the 2nd list would select or deselect from the first list. to solve it I passed an id down through props and then appended the id to the index to make it unique.