javascript - 需要触发通过映射函数reactjs编写的元素数组中的单个元素

标签 javascript arrays reactjs function dictionary

class Services extends Component {
    constructor(props) {
        super(props);
        this.state = {showoffer: false};
      }
      showOffers=( )=>{  
        this.setState({showoffer: !this.state.showoffer});
      }
    render() {  
        return (
            <div className="OSServicesContainer">
            <img className="OSlogomark" src={logomark} alt="logo mark" />
                <article className="OssHeadingText">OOM INTERIORS OFFERS</article>
                {offersdata.map((offers,index)=>{
                return   ( <div key={index} className="OssoffersContainermain">
                                <div className="OssoffersContainer">
                                    <div className="OssofferHeadingmain">
                                        <article className="OssofferHeading">{offers.heading}</article>
                                    </div> 
                                        <article className="OssofferText">{offers.subheading}</article> 
                                    <div className="OssofferViewbtnmain">
                                        <article key={index} className="OssofferViewbtn" onClick={this.showOffers}>{this.state.showoffer?"View Less":"View More"}</article>
                                    </div>
                                </div>
                                {!this.state.showoffer?
                                    null:
                                <div className="OssOfferSubCompmain">
                                    {offers.offersub.map((offer,key) =>{
                                        return <OssOfferSubComp ofrtext={offer.text} ofrsubtext={offer.subtext} />
                                    })}
                                </div>}
                            </div>    
                                )
                })}
            </div>);
    }
}

export default Services;

上面是我的代码 我想调用 showoffer 函数并仅更新单击的元素 请问我该怎么做才能触发所有元素 如何触发单个元素??

最佳答案

你可以试试这样: `类服务扩展组件{ 构造函数( Prop ){ super ( Prop ); this.state = {showoffer: 0};

    showOffers = ( offerIndex ) => {  
        this.setState({showoffer: offerIndex});
    }

    hideOffers = () => {
        this.setState({showoffer: 0});
    }

    render() => {
       ... 
       <div className="OssofferViewbtnmain">
           <article key={index} onClick={ () => this.showOffers(index) }> 
               {this.state.showoffer?"View Less":"View More"}
           </article>
       </div>
       ...
       { 
           this.state.showOffer && this.state.showOffer === index
           ? // then show it
           : ''
        }
    }`

关于javascript - 需要触发通过映射函数reactjs编写的元素数组中的单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268723/

相关文章:

reactjs - React-Bootstrap 全屏模态对话框

javascript - 将现有的 Div 放入 DOM div?

javascript - 2个链接ID之间的CSS线

Javascript - 按值删除数组项

javascript - 如何取消设置数组中的条目?

javascript - 神秘 react 错误 "unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering."

在移动设备上使用 Web Audio API 进行 Javascript 节拍检测

javascript - 将 HTML DOM 包含到 addEventListener() 中

arrays - 寻找超快数据存储来执行相交操作

javascript - 通量(替代) : ReferenceError: localStorage is not defined