【发布时间】:2021-06-20 15:53:18
【问题描述】:
如何使用 onChange 或 onClick 从点击的项目中获取 id,我想推送用户点击的特定项目。这完全是错误的方式吗?
我的代码:
import React, { useState } from "react";
import "antd/dist/antd.css";
import { Select } from "antd";
import { useHistory } from "react-router-dom";
function EventsSection() {
const { Option } = Select;
const history = useHistory();
const anarray = [
{ name: "obama", address: "usa", id: "81" },
{ name: "obama", address: "usa", id: "86" },
{ name: "putin", address: "russia", id: "91" },
{ name: "erdogan", address: "turkey", id: "31" },
{ name: "jebordoq", address: "alien", id: "29" },
];
const changed = (event: any) => {
history.push(`/Detail/${event.id}`);
};
return (
<section>
<Select
className="senderId"
style={{ width: "100%" }}
placeholder="Hide Me"
onChange={changed}
open={true}
listHeight={350}
>
{anarray.map((item, idx) => (
<Option
style={{ width: "100%", height: "100%" }}
className="options"
key={idx}
value={item.id}
>
{item.name}
<br></br>
{item.address}
<br></br>
</Option>
))}
</Select>
</section>
);
}
export default EventsSection;
如果需要可以问我
【问题讨论】:
-
您能否也添加您的路线详细信息?你的
react-router组件。 -
这是路径
-
console.log 在您更改的函数中记录您的事件并从那里检查。
-
使用该代码我什么也得不到
标签: javascript html reactjs select