简体   繁体   English

反应钩子 - OnClick 只显示点击的项目

[英]React hooks - OnClick show only the clicked item

I want that if I click on the button for one of those users, a second Ajax call get triggered and shows only that user, which is clicked.我希望如果我单击其中一个用户的按钮,则会触发第二个 Ajax 调用并仅显示该用户,该用户被单击。

For this I tried the following:为此,我尝试了以下方法:

import React, { useState, useEffect } from 'react'

function UserTile ({ setID, newID }) {    
  const [resources, setResources] = useState([])

  const fetchResource = async (setID) => {
    const response = await axios.get(
      'https://api.randomuser.me/'
    )
    console.log(response.data.results)
    setResources(response.data.results)
  }

  useEffect(() => {
    fetchResource()
  }, [])

  resources.map((item) => {
    if (item.login.uuid === newID) {
      return (
        <div className='card__item'>
          <h2 className='card__title'>{item.name.first} {item.name.last}</h2>
          <button className='btn--tile' onClick={() => setID(null)}>Back to overview</button>
              Details
        </div>
      )
    }
  })
}

export default UserTile

But then if I click on the button of one of the users, instead of seeing only that user, I get the following error:但是,如果我单击其中一个用户的按钮,而不是只看到该用户,我会收到以下错误:

Uncaught Error: UserTile(...): Nothing was returned from render.未捕获的错误:UserTile(...):渲染没有返回任何内容。 This usually means a return statement is missing.这通常意味着缺少 return 语句。 Or, to render nothing, return null.或者,不渲染任何内容,返回 null。

What is wrong with my code?我的代码有什么问题?

I didn't understand what you tried to achieve by the question you asked.我不明白您提出的问题试图达到什么目的。 But by the looks for your code, it should be optimized and done the following way!但是通过查找您的代码,它应该被优化并按照以下方式完成!


    import React, { useState, useEffect, useCallback, memo } from "react";
    import axios from "axios";

    const UserTile = ({ setID, newID }) => {
      const [resources, setResources] = useState([]);

      const fetchResource = useCallback(async () => {
        const response = await axios.get("https://api.randomuser.me/");
        setResources(response.data.results);
      }, []);

      useEffect(() => {
        fetchResource();
      }, [fetchResource]);

      const filteredResources = resources.filter(item => item.login.uuid === newID);

      if (filteredResources.length === 0) {
        return null;
      }

      return (
        <div className="card__item">
          <h2 className="card__title">
            {filteredResources.name.first} {filteredResources.name.last}
          </h2>
          <button className="btn--tile" onClick={() => setID(null)}>
            Back to overview
          </button>
          Details
        </div>
      );
    };

    export default memo(UserTile);

You should have at this point你应该在这一点上

  resources.map((item) => {
    if (item.login.uuid === newID) {
      return (
        <div className='card__item'>
          <h2 className='card__title'>{item.name.first} {item.name.last}</h2>
          <button className='btn--tile' onClick={() => setID(null)}>Back to overview</button>
              Details
        </div>
      )
    }
  })

replaced with替换为

return (<>
  resources.map((item) => {
    if (item.login.uuid === newID) {
      return (
        <div className='card__item'>
          <h2 className='card__title'>{item.name.first} {item.name.last}</h2>
          <button className='btn--tile' onClick={() => setID(null)}>Back to overview</button>
              Details
        </div>
      )
    }
  })
</>)

and probably use filter instead of map并且可能使用过滤器而不是 map

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

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