[ListView] renderRow pass state to prop
Hi all, I'm trying to set scrollY prop with a state value updated by the onscroll function. Is this the correct way of doing this? I need to pass the scrollY offset to my component. I'm able to get a value on my component but it doesn't update as I scroll.
renderRow: function (rowData, sectionID, rowID) {
return (
<VideoCard key={rowData.id} data={rowData} rowid={rowID} autoplay={this.state.autoplay} scrollY={this.state.contentOffset} navigator={this.props.navigator}/>
);
},
Hey, this question may be more appropriate and get answered faster on StackOverflow.
You may investigate having to include the contentOffset in the rowHasChanged calculation? Which may be why your new state isn't being passed in?
Agreed re: StackOverflow - we're more focused on bugs and features here unfortunately (wouldn't that be sweet if we also had the time/resources to answer all questions?) - people on StackOverflow will get back to you much more quickly! Closing, let us know if you don't have any luck on StackOverflow @yamill
@yamill were you able to figure this out?
@arilitan yes - i was able to get this to work properly.
@yamill do you mind pointing me to some sample code. I am trying to do something very similar - I want to update a component in the renderRow function every time the scrollOffset changes. I can't figure out how to get the row to update onScroll. Thanks
More specifically I am rendering a ListView where the rows are videos. I want the most visible row to play when scrolled and for all others to be paused. Seems like you are doing something similar.
@arilitan ahh yes - looks like you are facing the same issue. I created an autoplay video feed as well.
I had to make a slight change to the react-native library to get it to update each time the scrolloffset changes - I think this is a bug because in older releases it was working without this little hack.
You will need to change ListView.js L342-L343: https://github.com/facebook/react-native/blob/master/Libraries/CustomComponents/ListView/ListView.js#L342-L343
to
var shouldUpdateRow = true;
Let me know how this works out for you.
@yamill thank you! that worked, it would have taken me forever to think to check the in the ListView.js. its updating the rows on scroll but its affecting performance. throttling it somehow in shouldComponentUpdate seems like it will fix that.
also storing contentOffset in the state of the listview component and setting the state on each onScroll event also seems to have an affect on render performance, did you notice that? Trying to figure out a way around that...
@arilitan glad it worked! yeah it definitely does take a performance toll - i ended up writing a debounce function.
Like this:
var SearchBox = React.createClass({
componentWillMount: function () {
this.delayedCallback = _.debounce(function (event) {
// `event.target` is accessible now
}, 1000);
},
onChange: function (event) {
event.persist();
this.delayedCallback(event);
},
render: function () {
return (
<input type="search" onChange={this.onChange} />
);
}
});
You can read more about this here - http://stackoverflow.com/questions/23123138/perform-debounce-in-react-js
will try that, thanks again!