React Hooks< select>< option> gt; 2个下拉问题

发布于 2025-02-13 18:37:17 字数 1822 浏览 0 评论 0原文

我是新手反应钩子。已经创建了一个小型的两滴应用程序。第二个下拉(DD)内容是根据第一个DD的选择填充的。我在此处发布的代码具有第一个DD显示A,B,C,D和第二个DD显示A1 -A3或B1 -B3等,具体取决于选择。

首次通过,一切都按预期工作。小故障发生在更改第一个DD中的值后显示第二个DD中的列表。示例:最初在第一个DD中选择“ A”,然后在第二个DD中选择“ A3”。然后回到第一个DD,然后选择“ B”。现在显示为“ B1 -B4”,但在顶部显示“ B3”。我希望在更改第一个DD选择后每次都在顶部显示最初的空白选项。

我花了几个小时研究这个。香草JS版本正常,所以我假设这是我对React甚至钩子缺少的东西?根据So和其他Web文章,我尝试使DDS成为“受控组件”,并使用了Select元素等上所选的,值和默认属性的多个变体,等等。它都没有修复此故障。

请提前感谢所有可以帮助我了解如何解决此问题的人!

链接到codesandbox.io演示: https://codesandbox.io/s/patient-wood-wood-wood-0op9uuu

下面的代码:

import React, { useEffect, useState } from 'react';
import './App.css';

function Test_1() {
  const [DD1, setDD1] = useState([]);
  const [DD2, setDD2] = useState([]);
  useEffect(() => { }, [DD2]);

  const dd_data_1 = ["a", "b", "c"];
  const dd_data_2 = [{name: "a", content: "a1"}, {name: "a", content: "a2"}, {name: "a", content: "a3"},
                     {name: "b", content: "b1"}, {name: "b", content: "b2"}, {name: "b", content: "b3"},
                     {name: "c", content: "c1"}, {name: "c", content: "c2"}, {name: "c", content: "c3"}];

 const zz = (e) => {
    let foo = dd_data_2.filter(x => x.name === e.target.value);
    setDD2(foo);
 };

return (
<div id="main">

<select name="dd_1" id="dd_1" onChange={zz} >
  <option value=""></option>
  {dd_data_1.map((x, i) => (
    <option key={i} value={x}>{x}</option>
   ))}
</select>

<select name="dd_2" id="dd_2">
    <option value=""></option>
    {DD2.map((x, i) => (
       <option key={i} value={x.content}>{x.content}</option> 
    ))}
 </select>
</div>
)}

export default Test_1;

I'm new to React hooks. Have created a small two-dropdown app. The second dropdown (DD) content is populated based on choice made from the first DD. The code I'm posting here has the first DD displaying a,b,c,d, and the second DD displaying a1 - a3, or b1 - b3, etc., depending on the choice.

First time through, everything works as expected. The glitch occurs in how the list in the second DD is displayed after changing the value in the first DD. Example: Initially choose "a" in the first DD, then "a3" in the second DD. Then go back to the first DD, and choose "b". "b1 - b4" is now displayed as expected, but "b3" is displayed at the top. I want the initial blank option to be displayed at the top every time after changing the first DD choice.

I've spent hours and hours researching this. A vanilla JS version works fine, so I'm assuming it's something I'm missing with React and perhaps hooks? Per SO and other web articles, I've tried making the DDs a 'controlled component', used multiple variations of the selected, value, and default properties on the select element, etc., etc. None of it fixes this glitch.

Thanks ahead of time to any and all who can help me understand how to fix this!

Link to codesandbox.io demo: https://codesandbox.io/s/patient-wood-0op9uu

Code below:

import React, { useEffect, useState } from 'react';
import './App.css';

function Test_1() {
  const [DD1, setDD1] = useState([]);
  const [DD2, setDD2] = useState([]);
  useEffect(() => { }, [DD2]);

  const dd_data_1 = ["a", "b", "c"];
  const dd_data_2 = [{name: "a", content: "a1"}, {name: "a", content: "a2"}, {name: "a", content: "a3"},
                     {name: "b", content: "b1"}, {name: "b", content: "b2"}, {name: "b", content: "b3"},
                     {name: "c", content: "c1"}, {name: "c", content: "c2"}, {name: "c", content: "c3"}];

 const zz = (e) => {
    let foo = dd_data_2.filter(x => x.name === e.target.value);
    setDD2(foo);
 };

return (
<div id="main">

<select name="dd_1" id="dd_1" onChange={zz} >
  <option value=""></option>
  {dd_data_1.map((x, i) => (
    <option key={i} value={x}>{x}</option>
   ))}
</select>

<select name="dd_2" id="dd_2">
    <option value=""></option>
    {DD2.map((x, i) => (
       <option key={i} value={x.content}>{x.content}</option> 
    ))}
 </select>
</div>
)}

export default Test_1;

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

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

发布评论

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

评论(1

向地狱狂奔 2025-02-20 18:37:17

它不起作用,因为您设置了 index dd2数组的索引,

{DD2.map((x, i) => (
   <option key={i} value={x.content}>{x.content}</option> 
))}

因此当您的DD2数组更改时,该选项仍然相同,因为它具有相同的键。因此,只需将您的钥匙更改为独特的内容即可识别列表中的项目

{DD2.map((x, i) => (
     <option key={x.content} value={x.content}>
         {x.content}
     </option>
))}

It's not working because you set key of option by index of DD2 array

{DD2.map((x, i) => (
   <option key={i} value={x.content}>{x.content}</option> 
))}

so when your DD2 array change, the option still be the same because it have the same key. So just change your key to something unique to identifies the items in the list

{DD2.map((x, i) => (
     <option key={x.content} value={x.content}>
         {x.content}
     </option>
))}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文