当从React中的项目列表中单击按钮时显示特定项目。

huangapple go评论61阅读模式
英文:

Show specific item when button clicked from list of items in React

问题

我有一个对象列表。组件遍历列表并为每个项目返回一个按钮。当点击按钮时,我想显示特定项目。使用下面的代码,当点击按钮时,所有项目都会显示出来。如何实现这个功能?

import './App.css';
import { useState } from "react";

const list_of_items = [{id:1, text:"lorem ipsum 1"}, {id:2, text:"lorem ipsum 2"}, {id:3, text: "lorem ipsum 3"}]

function App() {
  const [seeItem, setSeeItem] = useState(false);
  function clickHandler(e) { 
    console.log("Button clicked ...", e);
    setSeeItem(prevState => !prevState);
  }
  return (
    <>
      <h1>我要测试单击列表项</h1>
      {list_of_items.map((item, index) => {
        return (
          <div key={index} style={{ margin: 70 }} onClick={clickHandler}>
            { seeItem && JSON.stringify(item)}
            <button style={{display:"block"}}>点击了哪个项目</button>
          </div>
        );
      })}
    </>
  );
}

export default App;

希望这对你有所帮助。

英文:

i have list of objects. Component iterate through list and returns a button for every item. I want to show a specific item when button clicked. With below code when click a button all items will be shown. How to do that?

import &#39;./App.css&#39;;
import { useState } from &quot;react&quot;;

const list_of_items = [{id:1, text:&quot;lorem ipsum 1&quot;}, {id:2, text:&quot;lorem ipsum 2&quot;}, {id:3, text: &quot;lorem ipsum 3&quot;}]

function App() {
  const [seeItem, setSeeItem] = useState(false);
  function clickHandler(e) { 
    console.log(&quot;Button  clicked ...&quot;, e);
    setSeeItem(prevState =&gt; !prevState);
  }
  return (
    &lt;&gt;
      &lt;h1&gt;Im going to test clicks to list of items&lt;/h1&gt;
      {list_of_items.map((item, index) =&gt; {
        return (
          &lt;div key={index} style={{ margin: 70 }} onClick={clickHandler}&gt;
            { seeItem &amp;&amp; JSON.stringify(item)}
            &lt;button style={{display:&quot;block&quot;}}&gt;Which item clicked?&lt;/button&gt;
          &lt;/div&gt;
        );
      })}
    &lt;/&gt;
  );
}

export default App;

答案1

得分: 1

以下是已翻译的内容:

如评论中所提到的,您需要一种列表来跟踪正在显示的项目。

创建状态:

const [seeItems, setSeeItems] = useState([]);

创建切换处理程序:

const toggleSeeItem = (index) => {
  setSeeItems((prevState) => {
    // 检查索引是否已经在数组中
    // 如果是,则从列表中删除它
    if (prevState.includes(index))
      return prevState.filter((item) => item !== index);
    // 否则将以前的数组与新的索引一起返回
    return [...prevState, index];
  });
};

toggleSeeItem添加到onClick,并提供index。您可以使用includes来检查当前项目是否应该显示。

{
  list_of_items.map((item, index) => {
    return (
      <div
        key={index}
        style={{ margin: 70 }}
        onClick={() => toggleSeeItem(index)}
      >
        {seeItems.includes(index) && JSON.stringify(item)}
        <button style={{ display: "block" }}>Which item clicked?</button>
      </div>
    );
  });
}
英文:

As mentioned in the comments you will need some kind of list to keep track of the items that are showing.

Create the state

const [seeItems, setSeeItems] = useState([]);

Create a toggle handler

const toggleSeeItem = (index) =&gt; {
  setSeeItems((prevState) =&gt; {
    // check if the index is already in the array
    // if so remove it from the list
    if (prevState.includes(index))
      return prevState.filter((item) =&gt; item !== index);
    // else the previous array with the new index
    return [...prevState, index];
  });
};

Add the toggleSeeItem to the onClick and provide the index. You can use includes to check if the current item should be shown.

{
  list_of_items.map((item, index) =&gt; {
    return (
      &lt;div
        key={index}
        style={{ margin: 70 }}
        onClick={() =&gt; toggleSeeItem(index)}
      &gt;
        {seeItems.includes(index) &amp;&amp; JSON.stringify(item)}
        &lt;button style={{ display: &quot;block&quot; }}&gt;Which item clicked?&lt;/button&gt;
      &lt;/div&gt;
    );
  });
}

huangapple
  • 本文由 发表于 2023年2月6日 02:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定