简体   繁体   English

ReactJS:通过 onChange/onClick 从 map 传递 ID

[英]ReactJS: Passing ID from map through onChange/onClick

How to get id from clicked items with onChange or onClick, I want to push that specific item which user clicks.如何使用 onChange 或 onClick 从单击的项目中获取 id,我想推送用户单击的特定项目。 Is this totally the wrong way?这完全是错误的方式吗?

my code:我的代码:

 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;

ask me if needed问我是否需要

Here is the functional code:这是功能代码:

import React from "react";
import { Select } from "andt";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  useParams,
  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 changeHandler = (name) => {
    history.push(`/Detail/${name}`);
  };

  return (
    <section>
      <Select
        className="senderId"
        style={{ width: "100%" }}
        placeholder="Hide Me"
        onChange={changeHandler}
        open={true}
        listHeight={350}
      >
        {anArray.map((item, idx) => (
          <Option
            style={{ width: "100%", height: "100%" }}
            className="options"
            key={idx}
            value={item.name}
          >
            {item.name}
            <br></br>
            {item.address}
            <br></br>
          </Option>
        ))}
      </Select>
    </section>
  );
}

function SlugHandler() {
  let { slug } = useParams();
  return <h3>I am {slug}!</h3>;
}

function BasicExample() {
  return (
    <Router>
      <Switch>
        <Route exact path="/">
          <EventsSection />
        </Route>
        <Route path="/Detail/:slug">
          <SlugHandler />
        </Route>
      </Switch>
    </Router>
  );
}

export default BasicExample;

If you want to keep id just replace the value prop for Options ie value={item.id} , also replace it from the handle function if that is something you are specifically looking for.如果您想保留id只需替换Options的 value 属性,即value={item.id} ,如果这是您特别寻找的东西,也可以从句柄 function 替换它。

Here is the functional codesandbox link:这是功能代码和框链接:

https://codesandbox.io/s/quizzical-thunder-nl2w5 https://codesandbox.io/s/quizzical-thunder-nl2w5

You can get the id of the element from the event object received by the event handler function.您可以从事件处理程序 function 收到的事件 object 中获取元素的 id。 Try using:尝试使用:

const changed = (event: any) => {
  history.push(`/Detail/${event.target.id}`); 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM