我有一個反應引導模式,我在其中映射我的數據,但問題是當我單擊模式時,所有模式都顯示相同的數據,它顯然是所有模式中數組的最後一項。我想動態生成模態並為不同模態顯示不同的數據。
const content: { cardimg: string, Name: string, Post: string, Nation: string, About: string, mod:string }[] = [ { "cardimg": `hello`, "Name": "someone 1", "Post": "Chairman & CEO", "Nation": "India", "About": "holds a degree of Executive MBA in Entrepreneurship from Rennes School of Business, France.", "mod": "one" }, { "cardimg": `world`, "Name": "someone 2", "Post": "Deputy CEO", "Nation": "India", "About": "holds a degree in MBA Finance, someone 2 has more than 7 years of experience in Customer Engagement Services.", "mod": "two" }, const Leadershipcard = (prop: props) =\> { const [modalShow, setModalShow] = useState(false) return ( <> {content.map((e) => <div className="card col-lg-6 p-0" style={{ "width": "18rem" } as React.CSSProperties}> <img src={`Assets\About\team\${e.cardimg}.webp`} className="card-img-top" alt="..." /> <div className="card-body"> <h5 className="card-title">{e.Name}</h5> <p className="card-text">{e.Post}</p> <a href="/" className="d-block fs-4"><i className="fa-brands fa-linkedin-in"></i></a> <button className="btn btn-outline-dark btn-dark rounded-0 text-bg-light mt-2 vi-more" onClick={() => setModalShow(true)} data-bs-toggle="modal" data-bs-target={`/mod${e.mod}`}>View More</button> ** <Modals show={modalShow} onHide={() => setModalShow(false)} Img={e.cardimg} Name={e.Name} Post={e.Post} Nation={e.Nation} About={e.About} mod={e.mod} />** </div> </div> )} </> ) } export default Leadershipcard
[Modal component that I used (Bold text above)](https://i.stack.imgur.com/Cg01P.png)
模態狀態必須維護在陣列中以追蹤特定模態。
我們可以透過在開啟和關閉模態時傳遞陣列索引來實現。