【问题标题】:Toggle button inside map地图内的切换按钮
【发布时间】:2021-04-02 00:06:49
【问题描述】:

我正在尝试在地图中设置一个切换开关,它通过一个按钮激活,现在我处理了卡片的每个项目之间的差异,但是有一个问题,因为状态发生了变化,我的意思是我没有更新它,状态不允许我激活几个按钮,我尝试将初始状态设为对象数组,但我无法上传数组的单个位置... 这是代码:

处理程序

 //hooks 
    const [detail, setDetail] = useState([{
        id: '',
        state: false,

    }]);

    const handleClick = (e, id) => {
        setDetail({ ...detail, id: id, state: !detail['state'] })
    }

地图

<div className={Styles.wrapper}>
                {
                    artistSales && artistSales.map(s => {

                        return (
                            <div className={Styles.align}>
                                <div className={Styles.card}>
                                    <div className={Styles.order}>
                                        <h2>Numero de orden: {s.id_order}</h2>
                                        <p>Estado de pago: {s.state === 'fullfilled' && 'pago realizado'} </p>
                                        <p>Cliente: {s.userId.name} {s.userId.lastname} </p>
                                        <p>Email:
                                <a href={`mailto:${s.userId.email}`}>{s.userId.email}</a>
                                        </p>
                                    </div>
                                    <div className={Styles.detail}>
                                        <p>Total: ${s.total_price}</p>
                                        <p>Fecha: {s.createdAt.slice(0, 10)}</p>
                                        <button value={s.id_order} onClick={(e) => handleClick(e, s.id_order)} className={Styles.btn}>Ver detalles</button>
                                    </div>

                                </div>
                                {detail.id === s.id_order && detail.state === true && <div>
                                    hello
                                </div>}----> this is what should be displayed when you click the button 

【问题讨论】:

    标签: reactjs react-hooks toggle


    【解决方案1】:

    您需要先找到一个匹配的 id。如果没有,则应将其添加到数组中,否则从匹配的新状态值中复制一份。

    您还可以创建一个函数来检查给定 id 是否具有特定状态值来呈现您的内容逻辑:

    const [details, setDetails] = useState([]);
    
    const handleClick = (e, id) => {
      setDetails(details => {
        // find index for matching detail
        const detailIndex = details.findIndex(d => d.id === id)
    
        // no match return new state
        if (detailIndex === -1) {
          return [...details, { id, state: true }]
        }
    
        // with match create copy details
        const nextDetails = [...details]
        
        // create copy of detail with a flipped state value
        const state = !nextDetails[detailIndex].state
        nextDetails[detailIndex] = { ...nextDetails[detailIndex], state }
    
        return nextDetails
      })
    }
    
    // function to get artist detail state
    const detailForArtist = ( id ) => {
      const detail = details.find(d => d.id === id)
      return detail?.state
    }
    
    
    { detailForArtist(s.id_order) && <div>hello</div> }
    

    【讨论】:

    • 非常感谢!这是完美的答案! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2021-02-10
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多