【问题标题】:Unable to get the key value of the item clicked in react hooks无法获取在反应挂钩中单击的项目的键值
【发布时间】:2020-10-07 13:33:20
【问题描述】:

在单击每个框时,我想获得相应框的tags。但是,当控制台记录打印标签时,它显示为未定义。我已经尝试过e.currentTarget.tagse.target.tags 这两种情况,但仍然没有得到正确的结果。有人可以帮忙吗?

演示链接:

https://codesandbox.io/s/serene-elgamal-lyyih?file=/src/App.js:87-1914

const blogData = [
  {
    id: 1,
    title: "Cypress tests",
    description: "Add the E2E cypress UI tests",
    createddate: "19 September 2020",
    tags: "cypress"
  },
  {
    id: 2,
    title: "Jmeter tests",
    description: "Performance test using Jmeter tool",
    createddate: "15 August 2020",
    tags: ["jmeter", "performance"]
  },
  {
    id: 3,
    title: "Basic Unix commands",
    description: "Learn basic unix commands in git bash",
    createddate: "10 July 2020",
    tags: "unix"
  },
  {
    id: 4,
    title: "Postman",
    description: "Api testing using postman",
    createddate: "1 June 2020",
    tags: ["postman", "api"]
  }
];
export default function App() {
  const [searchResults, setSearchResults] = useState([]);
  const [showColor, setShowColor] = useState("");
  const [findTag, setFindTag] = useState("");
  useEffect(() => {
    setSearchResults(blogData);
  }, [blogData]);

  const randomizedHex = (e) => {
    setFindTag(e.currentTarget.tags);
    console.log("Print tag:", findTag);
    const randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
    setShowColor(randomColor);
  };

  return (
    <div className="App">
      <div className="wrap">
        {searchResults.map((blog) => (
          <article onClick={randomizedHex} className="equalBox">
            <section>
              <span key={blog.id}> {blog.id} </span>
            </section>
            <section>
              <div key={blog.tags}>{blog.tags}</div>
            </section>
          </article>
        ))}
      </div>
    </div>
  );
}

【问题讨论】:

    标签: reactjs react-hooks


    【解决方案1】:

    代码需要将blog对象传递给回调:

      const randomizedHex = (blog) => {
        setFindTag(blog.tags);
        console.log("Print tag:", findTag);
        const randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
        setShowColor(randomColor);
      };
    
      return (
        <div className="App">
          <div className="wrap">
            {searchResults.map((blog) => (
              <article onClick={() => { randomizedHex(blog); }} className="equalBox">
                <section>
                  <span key={blog.id}> {blog.id} </span>
                </section>
                <section>
                  <div key={blog.tags}>{blog.tags}</div>
                </section>
              </article>
            ))}
          </div>
        </div>
      );
    

    【讨论】:

    • 你说得对!您也可以在没有{} 的情况下调用onClick={() =&gt; randomizedHex(blog)} 之类的函数。
    • @dazs 是的,虽然从 onclick 事件侦听器返回 false 很重要,所以我避免从箭头函数隐式返回值,除非我真的想要返回值。
    • @RossAllen 谢谢,如果我的博客有一点不同的 JSON 结构,我们可以直接从中得到tags 吗?
    • @soccerway 我不认为我理解您的后续问题。您可以发布一个新问题并在此处链接吗?
    • 谢谢艾伦,我确实有一个代码沙盒链接要分享,但我还没有提出新问题。它的问题相同,但博客对象结构不同。 codesandbox.io/s/infallible-resonance-fogf9?file=/src/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多