score:209
use the extradata
property on your flatlist component.
as the documentation states:
by passing
extradata={this.state}
toflatlist
we make sureflatlist
will re-render itself when thestate.selected
changes. without setting this prop,flatlist
would not know it needs to re-render any items because it is also apurecomponent
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:
set extra data to a boolean value.
extradata={this.state.refresh}
toggle the value of boolean state when you want to re-render/refresh list
this.setstate({ refresh: !this.state.refresh })
Source: stackoverflow.com
Related Query
- How to render AdMob banner in React Flatlist between items?
- How to loop and render elements in React.js without an array of objects to map?
- How to render react components by using map and join?
- How to create dynamic href in react render function?
- How to render a multi-line text string in React
- How to render an array of objects in React?
- How can I render repeating React elements?
- How do I render sibling elements without wrapping them in a parent tag?
- How to Call a Function inside a Render in React/Jsx
- How can I render HTML from another file in a React component?
- How do I render Markdown from a React component?
- How do you make the ListHeaderComponent of a React Native FlatList sticky?
- React JSX, how to render text with a single quote? Example <p>I've</p>
- How to render rectangles in a D3 grouped bar chart using React Faux DOM?
- How to focus something on next render with React Hooks
- how to render child components in react.js recursively
- React: how to load and render external html file?
- How to safely render html in react?
- How to render a HTML comment in React?
- How to wait for AJAX response and only after that render the component?
- React.js How to render component inside component?
- How to avoid bind or inline arrow functions inside render method
- How to async await in react render function?
- How to get the data from React Context Consumer outside the render
- Facebook React.js: how do you render stateful components on the server?
- React, how to access the DOM element in my render function from the same component
- How to render (print) JSX as String?
- How to create two columns with space beetwen in react native - flatList
- How to keep scroll position using flatlist when navigating back in react native ?
- How to dynamically import SVG and render it inline
More Query from same tag
- Can I make a functional component in Hyperstack?
- Retrieve firebase collection based on uid in React.js
- How to make tsconfig work with webpack provide plugin?
- × Error: Header(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- How to make GridList face vertically in One Row with React and materialUI on Mobile
- JavaScript typescript formData append empty Array
- replace nested for loop with array helpers - finding priority element
- Update state in object inside other object, React js
- Show header for all pages except home page in ReactJS
- Getting photo from array by path with firebase
- Download file from the server Laravel and reactjs
- Can't seem to pass return of React custom hook directly to React context
- eslint-plugin-react/require-render-return for functional components
- react component not re-rendering on state change from parent component firebase
- React function on pageload
- How to parse a HTML template from Json and populate the data
- Jest expected mock not called (redux component)
- React/API/Chart.js: Expected an assignment or function call and instead saw an expression
- ReactJS reconciliation when a parent DOM node changes type
- React import background image
- How to close window opened by window.open?
- React Redux with Typescript, Property Not Available in Reducer from ActionType
- How to test onClick() funct and useState hooks using jest and enzyme
- React JS shopping cart add functions
- Get the response from the dispatch - React Redux
- Group and sum of an array of objects based on number key
- Tell jest to ignore firebase/* when testing
- Using a remote console in a Typescript app with for an Office Add-In
- React trying to use .map to populate data from multiple APIs into the same mapped container
- react-native start giving Invalid regular expression invalid error