React 下拉值冲突

发布于 2025-01-19 20:55:50 字数 2136 浏览 0 评论 0原文

我有一个很好的下拉列表,可以检查点击值是否相同,这要归功于@david: https://codesandbox.io/s/laughing-leftpad-sepuly?file=/src/app/app/app.js

我使用下拉列表来更新参数,我在parter carts in parter carts y ye parter carts in parten成分。这是通过SETVAL(ID)通过单击函数发送给父的。问题是,当我需要使用多个状态时,下拉次数断开的功能。

const ExploreCategoriesDropdown = ({ Title, param, setVal }) => {

    const [dropdownOpen, setDropdownOpen] = useState(false);
    const [titleName, setTitleName] = useState(Title);

    const [data, setData] = useState([]);

    useEffect(() => {
        APILocalData(param)
            .then((res) => {
                setData(res.data)
            })
    }, [])

    function DropdownComponent() {

        function clicked(name, id) {
            if (name === titleName) {
                console.log("Same Clicked!");
                setTitleName(Title) //When same val is clicked, titleName should default
                setVal() //send empty parameter when value cleared
            } else {
                console.log("Different Clicked!");
                setTitleName(name);
                setVal(id) //send parameter value based on id prop
            }
        }

        return (
        <Dropdown
            isOpen={dropdownOpen}
            toggle={() => setDropdownOpen(!dropdownOpen)}
        >
        <DropdownToggle className="btn_white_dark_border dropdown_border" button caret>
            <span className="pe-4">{titleName}</span>
        </DropdownToggle>
        <DropdownMenu>
                {data.map((v, i) => (
                <DropdownItem
                    key={i}
                    value={v.Name}
                        onClick={() => clicked(v.Name, v.Id)
                    }
                >
                {v.Name}
                </DropdownItem>
            ))}
        </DropdownMenu>
        </Dropdown>
        );
    }

当我添加setVal()时,出现了问题。我是否应该利用Usestate来处理这些互动?

I have a nicely working Dropdown that checks whether clicked values are the same, thanks to @David: https://codesandbox.io/s/laughing-leftpad-sepuly?file=/src/App.js

I use the Dropdown to update parameters with which I render cards in a parent component. This is sent to the parent through clicked function through setVal(id). The issue is, when I need to use more than one state, the functionality of the Dropdown breaks.

const ExploreCategoriesDropdown = ({ Title, param, setVal }) => {

    const [dropdownOpen, setDropdownOpen] = useState(false);
    const [titleName, setTitleName] = useState(Title);

    const [data, setData] = useState([]);

    useEffect(() => {
        APILocalData(param)
            .then((res) => {
                setData(res.data)
            })
    }, [])

    function DropdownComponent() {

        function clicked(name, id) {
            if (name === titleName) {
                console.log("Same Clicked!");
                setTitleName(Title) //When same val is clicked, titleName should default
                setVal() //send empty parameter when value cleared
            } else {
                console.log("Different Clicked!");
                setTitleName(name);
                setVal(id) //send parameter value based on id prop
            }
        }

        return (
        <Dropdown
            isOpen={dropdownOpen}
            toggle={() => setDropdownOpen(!dropdownOpen)}
        >
        <DropdownToggle className="btn_white_dark_border dropdown_border" button caret>
            <span className="pe-4">{titleName}</span>
        </DropdownToggle>
        <DropdownMenu>
                {data.map((v, i) => (
                <DropdownItem
                    key={i}
                    value={v.Name}
                        onClick={() => clicked(v.Name, v.Id)
                    }
                >
                {v.Name}
                </DropdownItem>
            ))}
        </DropdownMenu>
        </Dropdown>
        );
    }

The problem arises when I add setVal(). I am clueless whether I should utilize useState also to handle those interactions?

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

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

发布评论

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

评论(1

似狗非友 2025-01-26 20:55:50

是的,您可以使用USESTATE挂钩存储选定的下拉套件:

const [selectionDropDownItemid,setSelectedDropdownItemid] = usestate(null);

以及在单击功能中比名称更好地比较ID。

另外,您可以将所有数据存储在一个状态下,

const [selectedDropdownItem, setSelectedDropdownItem] = useState(null);

然后将其存储在单击函数中:

setSelectedDropdownItem({ name: name,  id: id });

Yes, you can store selected dropdown item in component state, using useState hook:

const [selectedDropdownItemId, setSelectedDropdownItemId] = useState(null);

and in the clicked function better compare ids than names.

Also you can store all data in one state, like this:

const [selectedDropdownItem, setSelectedDropdownItem] = useState(null);

and then in clicked function:

setSelectedDropdownItem({ name: name,  id: id });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文