如何返回多次相同的组件但具有独立状态?

发布于 2025-02-08 02:21:46 字数 1209 浏览 2 评论 0原文

嗨,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}/>
)
}

我通过setnamesmain()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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2025-02-15 02:21:46

这个问题措辞错误(这与状态无关,与道具以及反应渲染方式无关)并且缺少信息,但是这里有一些可能会有所帮助的信息:

  • 钥匙不会更改前面的任何内容-结尾;它们是一种工具React用途来提高后端的性能。 单击此处以阅读文档以获取有关钥匙的更多信息以及如何反应。使用它们。
  • React组件是JavaScript函数,因此只能返回1件。您共享的代码样本将在React(或JavaScript)中不起作用。为了返回多个项目,可以将它们包裹在数组或React片段中。考虑此示例:
    render() {
      return (
        <React.Fragment>
          <ChildA />
          <ChildB />
          <ChildC />
        </React.Fragment>
      );
    }

如果要使下拉列表的外观和/或功能不同,则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:

  • Keys don't change anything on the front-end; they are a tool React uses to improve performance on the back-end. Click here to read the docs for more information on keys and how React uses them.
  • React components are JavaScript functions and can therefore only return 1 thing. The code sample you shared would not work in React (or JavaScript). In order to return multiple items, it is possible to wrap them in an array or a React Fragment. Consider this example:
    render() {
      return (
        <React.Fragment>
          <ChildA />
          <ChildB />
          <ChildC />
        </React.Fragment>
      );
    }

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 of Component1 are being given the same name property, which is why they probably look and behave identically.

妄司 2025-02-15 02:21:46

(我想发表评论,但不能)

我不确定(我必须看到完整的代码),但我认为:

return(
    <Component1 key="compare" name={name}/>
    <Component1 key="compared" name={name}/>
)

必须

return(
    <Component1 key="compare" name={name1}/>
    <Component1 key="compared" name={name2}/>
)

这样的方式它们不共享相同的变量

(i would like can comment but i can't)

i am not sure (i must see the complete code) but i think that:

return(
    <Component1 key="compare" name={name}/>
    <Component1 key="compared" name={name}/>
)

must be

return(
    <Component1 key="compare" name={name1}/>
    <Component1 key="compared" name={name2}/>
)

this way they don't share the same variable

流云如水 2025-02-15 02:21:46

您在哪里打电话并设置名称?由于您将相同的道具名称传递给两个人,它们将是相同的。
要么在组件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.

好菇凉咱不稀罕他 2025-02-15 02:21:46

我也有类似的情况,但就我而言,这是我需要使独特的ID。
我有一个组件,该组件在React Bootstrap的组中渲染了一个受控toggleButtons的列表。我想显示2个按钮列表,既包含在不同类别下可以选择的名称列表。最后,我必须设置ID而不是密钥。我最初将其设置为列表的索引。但是,当然,这两个列表都具有相同的索引,因此只有第一个具有状态并按下第二个列表将从第一个列表中选择或取消选择。为了解决它,我通过道具向下传递了一个ID,然后将ID附加到索引中以使其与众不同。

      function ToggleList({ variant = 'secondary', vertical = true, ...restProps }) {
  var items = restProps.list ? restProps.list.map((subject, index) => {
    return <ToggleButton
      className="mb-2"
      size="sm"
      id={`${restProps.id}-${index}`}
      type="checkbox"
      value={subject}
      variant={variant}>
      {subject}
    </ToggleButton>;
  }) : '';

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.

      function ToggleList({ variant = 'secondary', vertical = true, ...restProps }) {
  var items = restProps.list ? restProps.list.map((subject, index) => {
    return <ToggleButton
      className="mb-2"
      size="sm"
      id={`${restProps.id}-${index}`}
      type="checkbox"
      value={subject}
      variant={variant}>
      {subject}
    </ToggleButton>;
  }) : '';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文