为了账号安全,请及时绑定邮箱和手机立即绑定

切换选择。

切换选择。

料青山看我应如是 2023-10-20 16:52:53
在有状态组件中,我有以下代码:const handleUserClick = (username, usermemo, userSelected) => {    const isUsername = userName === username ? "Select a user" : username;    const isUsermemo = userMemo === usermemo ? null : usermemo;    setUserName(isUsername);    setUserMemo(isUsermemo);    setSelectedUser(userSelected);  };在无状态组件中,我有以下代码:const selectUserClick = ({ target }) => {    let selectedUserIndex = USERS_DATA[target.value];    let username = `${selectedUserIndex.first_name} ${selectedUserIndex.last_name}                   - ${selectedUserIndex.occupation}`;    let usermemo = `"${selectedUserIndex.catch_phrase}"`;    userSelected = target.classList.toggle("selected");    getUser(username, usermemo, userSelected);`enter code here`  };
查看完整描述

3 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

尝试将 User.js 更改为以下内容

className 根据选择的用户动态设置

Referlink -代码沙箱

import React from "react";

import { USERS_DATA } from "./data.js";


export default function User({ getUser, userSelected }) {

  const selectUserClick = ({ target }) => {

    let selectedUserIndex = USERS_DATA[target.value];

    let username = `${selectedUserIndex.first_name} ${selectedUserIndex.last_name} 

                      - ${selectedUserIndex.occupation}`;

    let usermemo = `"${selectedUserIndex.catch_phrase}"`;


    // userSelected = target.classList.toggle("selected");


    getUser(username, usermemo, selectedUserIndex);

  };


  return (

    <div className="users-container">

      <ul>

        {USERS_DATA.map((user, index) => (

          <li

            key={index}

            value={user.id - 1}

            onClick={selectUserClick}

            className={`users ${

              userSelected && user.id === userSelected.id ? "selected" : ""

            }`}

          >

            {user.user_name}

          </li>

        ))}

      </ul>

    </div>

  );

}


查看完整回答
反对 回复 2023-10-20
?
Qyouu

TA贡献1786条经验 获得超11个赞

在您的情况下,只要您不需要一次选择多个选项,这样的事情就可以工作。运行一个函数,获取所有附加有“selected”类名的用户,并在进行新选择之前将其关闭。


const selectUserClick = ({ target }) => {

  resetHighlights();


  let selectedUserIndex = USERS_DATA[target.value];

  let username = `${selectedUserIndex.first_name} ${selectedUserIndex.last_name} - ${selectedUserIndex.occupation}`;

  let usermemo = `"${selectedUserIndex.catch_phrase}"`;


  userSelected = target.classList.toggle("selected");


  getUser(username, usermemo, userSelected);

};


function resetHighlights() {

  var arr = document.getElementsByClassName("users");


  for (var x = 0; x < arr.length; x++) {

    if (arr[x].classList.contains("selected")) {

      arr[x].classList.toggle("selected");

    }

  }

}


查看完整回答
反对 回复 2023-10-20
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

另一种方法是:


我在代码中添加了简短的注释


export default function User({

  getUser,

  userSelected

}) {

  const selectUserClick = ({

    target

  }) => {

    let selectedUserIndex = USERS_DATA[target.value];

    let username = `${selectedUserIndex.first_name} ${selectedUserIndex.last_name} 

                      - ${selectedUserIndex.occupation}`;

    let usermemo = `"${selectedUserIndex.catch_phrase}"`;


    // get all li elements of selected users

    let usersEl = document.getElementsByClassName("users selected");

    let usersList = [...usersEl];


    // assigns exisiting class to toggled class beforre selection

    usersList.forEach((element) => {

      element.className = "users";

    });


    // style only current selected el

    userSelected = target.classList.toggle("selected");


    getUser(username, usermemo, userSelected);

  };


查看完整回答
反对 回复 2023-10-20
  • 3 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信