React Native FlatList, nagging for key when rendering components with a switch -


here's component code witch fetches items , injects advertisement every 20 cards, uses pagination , cards being added existing data when user reaches bottom of list:

// every 20 cards, inject advertisment var moduluscount = 0; if ((this.state.labels.length - modcount) % 20 === 0) {                     this.state.labels.push({type: 'ad'});                     moduluscount++;                 }  _renderitem = ({item}) => {     switch (item.type) {         case 'label':             return <card key={item._id} style={styles.card}>                 <cardtitle title={item.description}/>                 <touchableopacity style={styles.image} onpress={() => this._showimage(item.imagepath, item.upvotes, item._id)} activeopacity={0.7}>                     <cardimage seperator={false} id={item._id} incolumn={false} source={{uri: item.imagepath}}/>                 </touchableopacity>             </card>;         case 'ad':             return (this.state.fbad && this.state.ads ?                 <view key={item._id}>                     <card style={styles.card}>                         <cardtitle title={'sponsored'}/>                         <bannerview                             placementid={placementid}                             type="large"                             style={{width: 100}}                             onpress={() => console.log('click')}                             onerror={this.onbanneraderror}                         />                     </card>                 </view>                 : null );         default:             return null;     } };               <view style={styles.view}>                     <flatlist                         data={this.state.labels}                         keyextractor={this._keyextractor}                         renderitem={this._renderitem}                         onscroll={this._onscroll}                         refreshing={this.state.refreshing}                         onrefresh={this.handlerefresh}                         onendreached={this.handleloadmore}                         onendreachedthreshold={0.1}                         onmomentumscrollbegin={() => {                             this.onendreachedcalledduringmomentum = false;                         }}                         removeclippedsubviews={true}                         listfootercomponent={this.renderfooter}                     />                 </view>             </view> 

everything works fine, ad displayed every 20 items, rn complains key warning: each child in array or iterator should have unique "key" prop.

there's going on key of 'ad' type, doing wrong? how can make 'ad' type key unique? tried several approaches adding shortid.generate() npm module , inject key when pushing in array this.state.labels.push({key: shortid.generate(), type: 'ad'}) , setting key={item.key} in card component no luck @ all.

please ensure item._id unique every item.


Comments

Popular posts from this blog

javascript - WinJS appendTextAsync producing scheduler errors -

minify - Minimizing css files -

Sockets with kotlin -