html 如何激活两个或更多按钮

yb3bgrhw  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(148)

我需要做用户卡,点击跟随按钮,它改变颜色和名称,并添加1以下.我收到这样的用户从API,12用户,我需要能够选择多个1用户,我怎么能做到这一点,我想不出来.
但是我不明白如何标记按钮以记录在商店中我的代码在那里我提出一个请求。然后我将其记录在“用户”中,并通过map,我渲染所有的卡

export const UserList = () => {
  const [users, setUsers] = useState([]);
  const [userId, setUserId] = useState(null);
  const [page, setPage] = useState(2);

  const getUsersFromAPI = async () => {
    const user = await fetchUsers();
    setUsers(user);
  };

  const fetchNextPage = async () => {
    setPage(prevPage => prevPage + 1);
    const usersNext = await nextPageUsers(page, 3);
    setUsers(prevUser => [...prevUser, ...usersNext]);
    if (page === 4) {
      setPage(1);
      await nextPageUsers(page, 3);
      return;
    }

    return usersNext;
  };

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

  
# const followind = (id, e) => {
# 
# const userIdx = users.findIndex(user => user.id === id);
# 
# if (userId === id) {
# setUserId(null);
# users[userIdx].followers--;
# 
# return;
#     }
# 
# setUserId(id);
# users[userIdx].followers++;
#   };

  return (
    <div>
      {users && (
        <ul className={css.list}>
          {users.map(user => {
            return (
              <li key={user.id} className={css.containerItem}>
                <div className={css.space}>
                  <button type="button" className={css.imageLogo}></button>
                  <div className={css.upperPart}></div>
                  <div className={css.middleLine}>
                    <img
                      className={css.userFoto}
                      src={user.avatar}
                      alt={user.name}
                    ></img>
                  </div>
                  <div className={css.lowerPart}>
                    <p className={css.ps}> {user.tweets} Tweets</p>

                    <p className={css.ps}>
                      {' '}
                      {user.followers.toLocaleString('en-US')}
                      Followers
                    </p>

                    ***<button
                      id={user.id}
                      className={
                        userId === user.id ? css.btnPress : css.btnFollow
                      }
                      type="button"
                      onClick={e => followind(user.id, e)}
                    >
                      {userId === user.id ? 'Following' : 'Follow'}
                    </button>***
                  </div>
                </div>
              </li>
            );
          })}
        </ul>
      )}
      {page <= 4 && (
        <button type="button" onClick={fetchNextPage}>
          Next
        </button>
      )}
    </div>
  );
};```
nzrxty8p

nzrxty8p1#

如果我正确理解了你的问题。。
你需要一个参数来表示“关注状态”
为此,您需要将新参数添加到users数组中的对象,例如,通过调用此参数“followingStatus”,并在单击时更改其布尔值
您需要将新数组返回到useState钩子

const [users, setUsers] = useState([
    {
      id: '1',
      followers: 0,
      ...
    },
    {
      id: '2',
      followers: 56,
      ...
    },
    {
      id: '3',
      followers: 1,
      ...
    },
    {
      id: '4',
      followers: 4,
      ...
    },
    {
      id: '5',
      followers: 9,
      ...
    },
  ]);
  const [usersFollowingIds, setUserFollowingIds] = useState({'2': true});
const followind = (id, followingStatus) => {
    setUserFollowingIds((prevState) => {
      const newState = {...prevState};

      if (followingStatus) {
        delete newState[id];
      } else {
        newState[id] = true;
      }

      return newState;
    });

    setUsers((prevState) => {
      return prevState.map((user) => {
        if (user.id === id) {
          return {
            ...user,
            followers: followingStatus ? --user.followers : ++user.followers,
          };
        }

        return user;
      });
    });
  };
{users.map((user) => {
  const followingStatus = usersFollowingIds[user.id];

  return (
    <button key={user.id} id={user.id} className={followingStatus ? css.btnPress : css.btnFollow} type="button" onClick={() => followind(user.id, followingStatus)}>
      {followingStatus ? 'Following' : 'Follow'}
    </button>
  );
})}

相关问题