score:209

Accepted answer

use the extradata property on your flatlist component.

as the documentation states:

by passing extradata={this.state} to flatlist we make sure flatlist will re-render itself when the state.selected changes. without setting this prop, flatlist would not know it needs to re-render any items because it is also a purecomponent and the prop comparison will not show any changes.

score:-1

just use:

onrefresh={true}

inside your flatlist component.

score:-1

flatlist's extradata wasn't working for me and i happened to be using a prop from redux. this sounded similar to issues from the comments in ed209's answer. i ended up manually calling setstate when i receive the prop.

componentwillreceiveprops(nextprops: stateprops) {
    if (this.props.yourprop != null && nextprops.yourprop) {
        this.setstate({ yourstate: processprop(nextprops.yourprop) });
    }
}


<flatlist
  data={this.state.yourstate}
  extradata={this.state.yourstate}
/>

for those of you using > react 17 use getderivedstatefromprops

score:0

i have replaced flatlist with sectionlist and it is updates properly on state change.

<sectionlist
  keyextractor={(item) => item.entry.entryid} 
  sections={section}
  renderitem={this.renderentries.bind(this)}
  rendersectionheader={() => null}
/>

the only thing need to keep in mind is that section have diff structure:

const section = [{
  id: 0,
  data: this.state.data,
}]

score:0

for me, the trick was extradata and drilling down into the item component one more time

state = {
  uniquevalue: 0
}

<flatlist
  keyextractor={(item, index) => item + index}
  data={this.props.photos}
  renderitem={this.renderitem}
  itemseparatorcomponent={this.renderseparator}
/>

renderitem = (item) => {
  if(item.item.selected) {
    return ( <button onpress={this.itempressed.bind(this, item)}>selected</button> );
  }
  return ( <button onpress={this.itempressed.bind(this, item)}>not selected</button>);
}

itempressed (item) {
  this.props.photos.map((img, i) => {
    if(i === item.index) {
      if(img['selected') {
        delete img.selected;
      } else {
        img['selected'] = true;
      }
      this.setstate({ uniquevalue: this.state.uniquevalue +1 });
    }
  }
}

score:0

put variables that will be changed by your interaction at extradata

you can be creative.

for example if you are dealing with a changing list with checkboxes on them.

<flatlist
      data={this.state.data.items}
      extradata={this.state.data.items.length * (this.state.data.done.length + 1) }
      renderitem={({item}) => <view>  

score:0

if we want the flatlist to know the data change both prop and state,we can construct an object referencing both prop and state and refresh the flatlist.

const hasproporstatechange = { propkeytowatch: this.props, ...this.state};
<flatlist data={...} extradata={this.hasproporstatechange} .../>

docs: https://facebook.github.io/react-native/docs/flatlist#extradata

score:0

in react-native-flatlist, they are a property called as extradata. add the below line to your flatlist.

 <flatlist
          data={data }
          style={flatliststyles}
          extradata={this.state}
          renderitem={this._renderitem}
       />

score:0

i am using functional component, in that i am using flatlist with redux data. i am managing all the state with redux store. here is the solution to update the flatlist data after the api call.

i was first doing like this:-

const data  = useselector((state) => state.address.address);


<flatlist
    style = {styles.myaddresslist}
    data = {data}
    renderitem = {renderitem}
    keyextractor = {item => item._id}
    listemptycomponent = {emptylist}
    itemseparatorcomponent={separatorwhite}
    extradata = {refresh}
    
    />

but the data was not re-rendering my flatlist data at all.

as a solution i did like given below:-

<flatlist
    style = {styles.myaddresslist}
    data = {useselector((state) => state.address.address)}
    renderitem = {renderitem}
    keyextractor = {item => item._id}
    listemptycomponent = {emptylist}
    itemseparatorcomponent={separatorwhite}
    />

i am passing the redux state directly to the flatlist datasource rather than allocating it to the variable.

thank you.

score:0

const [itemselected, setitemselected] = setstate(null);
....
const flatlistitem = (item) => {
    return (
        <touchableopacity onpress={() => setitemselected(item.id)}>
            <view style={ (itemselected === item.id) ? style.itemwrapperactive : style.itemwrapper }>
                <text>{ item.label }</text>
            </view>
        </touchableopacity>
    )
}
....
<flatlist
    itemseparatorcomponent={() => <view style={{ width: 20 }} />}
    data={ flatlistdata }
    renderitem={ ({item}) => flatlistitem(item) }
    keyextractor={ (item) => item.id }
    extradata={ itemselected }
/>

score:0

for those using redux, i used extradata prop and added loading there what i also did was i created a custom hook called useprevious

import {useeffect, useref} from 'react';

export const useprevious = <t>(value: t): t | undefined => {
  const ref = useref<t>();
  useeffect(() => {
    ref.current = value;
  });
  return ref.current;
};

and used it like this on my item.

const prevdata = useprevious({data});

//codes...

useeffect(() => {
    if (prevdata?.data === undefined) {
      return;
    }
    if (prevdata?.data?.someid !== data?.someid) {
      // do something.
      showsubchildren(false)
    }
  }, [data?.auctionname, prevdata, resetitemstates]);

so what basically happened here was, your item will check if data is from undefined (first time). then it will check if some data property has changed and if it has, you should do something.

score:0

doing like below will instantly make the flatlist item change reflected instantly. you can do this similarly in component also if you are not using redux.

  let list = object.assign([], state.auctionlist);
  let objindex;
  //find index of specific object using findindex method.
  objindex = list.findindex(
    obj => obj.auction_vehicles.vehicle_id == payload.vehicle_id,
  );

  // //update object's property.
  list[objindex].auction_vehicles.time_left = payload.offer_expiry_time;
  list[objindex].auction_vehicles.starting_price = payload.amount;
  list[objindex].auction_vehicles.bidding_status =
    payload.highest_bidder_flag;

  return { ...state, auctionlist: list };

score:1

i solved this problem by adding extradata={this.state} please check code below for more detail

render() {
    return (
      <view style={styles.container}>
        <flatlist
          data={this.state.arr}
          extradata={this.state}
          renderitem={({ item }) => <text style={styles.item}>{item}</text>}
        />
      </view>
    );
  }

score:1

in this example, to force a re-render, just change the variable machine

const [selected, setselected] = usestate(machine)

useeffect(() => {
    setselected(machine)
}, [machine])

score:3

just an extension on the previous answers here. two parts to ensure, make sure that you add in extradata and that your keyextractor is unique. if your keyextractor is constant a rerender will not be triggered.

<flatlist
data={this.state.allarray}
extradata={this.state.refresh}
renderitem={({ item,index })=>this.renderphoto(item,index)}
keyextractor={item => item.id}
>
                                    </flatlist>

score:4

if you are going to have a button, you can update the data with a setstate inside the onpress. setstate will then re-render your flatlist.

score:5

after lots of searching and looking for real answer finally i got the answer which i think it is the best :

       <flatlist
      data={this.state.data}
      renderitem={this.renderitem}
      listheadercomponent={this.renderheader}
      listfootercomponent={this.renderfooter}
      itemseparatorcomponent={this.renderseparator}
      refreshing={this.state.refreshing}
      onrefresh={this.handlerefresh}
      onendreached={this.handleloadmore}
      onendreachedthreshold={1}
      extradata={this.state.data}
      removeclippedsubviews={true}
      **keyextractor={ (item, index) => index }**
              />
    .....

my main problem was (keyextractor) i was not using it like this . not working : keyextractor={ (item) => item.id} after i changed to this it worked like charm i hope this helps someone.

score:8

ok.i just found out that if we want the flatlist to know the data change outside of the data prop,we need set it to extradata, so i do it like this now:

<flatlist data={...} extradata={this.state} .../>

refer to : https://facebook.github.io/react-native/docs/flatlist#extradata

score:22

oh that's easy, just use extradata

you see the way extra data works behind the scenes is the flatlist or the virtualisedlist just checks wether that object has changed via a normal oncomponentwillreceiveprops method.

so all you have to do is make sure you give something that changes to the extradata.

here's what i do:

i'm using immutable.js so all i do is i pass a map (immutable object) that contains whatever i want to watch.

<flatlist
    data={this.state.calendarmonths}
    extradata={map({
        foo: this.props.foo,
        bar: this.props.bar
    })}
    renderitem={({ item })=>((
        <customcomponentrow
            item={item}
            foo={this.props.foo}
            bar={this.props.bar}
        />
    ))}
/>

in that way, when this.props.foo or this.props.bar change, our customcomponentrow will update, because the immutable object will be a different one than the previous.

score:65

for quick and simple solution try:

  1. set extra data to a boolean value.

    extradata={this.state.refresh}

  2. toggle the value of boolean state when you want to re-render/refresh list

    this.setstate({ 
        refresh: !this.state.refresh
    })
    

Related Query

More Query from same tag