目前,我正在创建一个用于自学react 的静态售票应用程序.我使用Bootstrap进行造型

when I select the ticket count it shows in Seat Select, enter image description here

const [select,setSelect]=useState(0);
const [seatCount,setSeatCount]=useState(select);
const [toggleSeat,setToggleSeat]=useState(false);

const ticketCountHandler=(e)=>{
    setSelect(e.target.value);
    console.log(e.target.value);
    setSeatCount(e.target.value)
  }

const ticketCount=Array.from({ length: 10 }, (_, index) => index + 1);
  

  const seatCountHandler=(e)=>{
    e.preventDefault();
    setToggleSeat(!toggleSeat); 

    if(!toggleSeat){
       setSeatCount(prev=>prev-1)
    }else{
      setSeatCount(prev=>prev+1)
    }
 }

seat count dropdown menu

{/*count inputl start */}
              <div className="col-4">
              <select id="count" value={select} onChange={ticketCountHandler} className='form-select'>
                  <option selected disabled hidden>Select Ticket Count(max 10)</option>
                  {ticketCount.map((value) => (
                  <option key={value} value={value}>
                           {value}
                      </option>
                   ))}
                  
                </select>
              </div>
              {/*count input end */}

seat selection model

<button type="button" class="btn form-control" data-bs-toggle="modal" data-bs-target="#seat-count">
  Select Seats
</button>


<div class="modal fade" id="seat-count" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Your Ticket Count {seatCount}</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div className="row">
          <div className="col-1"></div>
          <div className="col-1 m-2"><button className='btn seat-btn disabled bg-danger'>100</button></div>
          <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={101} onClick={seatCountHandler}>101</button></div>
          <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={102} onClick={seatCountHandler}>102</button></div>
          <div className="col-1"></div>
          <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={103} onClick={seatCountHandler}>103</button></div>
          <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={104} onClick={seatCountHandler}>104</button></div>
          <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={105} onClick={seatCountHandler}>105</button></div>
          <div className="col-1"></div>
          <div className="col-1"></div>
        </div>

现在,当我单击一个按钮时,所有按钮都会突然变色,即使在单击多个按钮后,计数也只更改一次.

enter image description here

我需要帮助来解决这个问题,自从上周我被困在这个问题上,try 了这么多相关的问题,使用chat gpt/bard,看了一些yt视频...仍然无法解决这个问题.

推荐答案

简明扼要的答案是,所有座位按钮都有一个通用的toggleSeat--无论何时 Select 一个座位,当toggleSeat变为真时,所有按钮都会应用bg-success的样式:

// Using the second group as an example, 
// they all rely on the same toggleSeat for className.
<div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={103} onClick={seatCountHandler}>103</button></div>
<div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={104} onClick={seatCountHandler}>104</button></div>
<div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeat?"bg-success":""}`} value={105} onClick={seatCountHandler}>105</button></div>

你有多种方法来解决这个问题,你当然可以把select状态变成Object,或者把toggleSeat状态变成array.

但我会为你提供另一个.


您可以使用Set()作为您的优势(代码解释):

// ...other useState()
const [toggleSeats, setToggleSeats] = useState(new Set());

const seatCountHandler=(e)=>{
    e.preventDefault();
    
    // Convert the value to a number (optional).
    let numberChagned = Number(e.target.value);
    // Check if the Set() has the number,
    if (!toggleSeats.has(numberChanged)) {
        setToggleSeats(prev => new Set(prev.add(numberChanged));
        setSeatCount(prev => prev - 1);
    } else {
        setToggleSeats(prev => {
            prev.delete(numberChanged);
            return new Set(prev);
        };
        setSeatCount(prev => prev + 1);
    }
}

// ...other functions

return (
    // ...other parts of jsx
    // Check for specific values in the Set for each button.
    <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeats.has(103) ? "bg-success" : ""}`} value={103} onClick={seatCountHandler}>103</button></div>
    <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeats.has(104) ? "bg-success" : ""}`} value={104} onClick={seatCountHandler}>104</button></div>
    <div className="col-1 m-2"><button className={`btn seat-btn ${toggleSeats.has(105) ? "bg-success" : ""}`} value={105} onClick={seatCountHandler}>105</button></div>
}

请注意,这个问题也发生在你setSelect() -目前它只会存储一个值;

通过使用Set(),您可以将select状态与toggleSeat状态相结合,而不是使用Objectarray:

const [selected, setSelected] = useState(new Set());
// remove the below one, and then apply the rest in the above codeblock:
// const [toggleSeat,setToggleSeat] = useState(false);

// ...other codes

Javascript相关问答推荐

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

传递一个大对象以在Express布局中呈现

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

我可以从React中的出口从主布局调用一个处理程序函数吗?

如何修复我的js构建表每当我添加一个额外的列作为它的第一列?

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

我怎么在JS里连续加2个骰子的和呢?

在浏览器中触发插入事件时检索编码值的能力

第三方包不需要NODE_MODULES文件夹就可以工作吗?

将异步回调转换为异步生成器模式

如何在JAVASCRIPT中合并两组对象并返回一些键

MongoDB受困于太多的数据

使用父标签中的Find函数查找元素

OnClick更改Json数组JSX中的图像源

如何在脚本编译后直接将RxJ模块导入浏览器(无需Angel、webpack、LiteServer)

从异步操作返回对象

Django模板中未加载JavaScript函数

使用静态函数保存 node 前的钩子

Reaction-在同一页内滚动导航(下拉菜单)