Skip to content

[FlatList] Add setNativeProps - #13529

Closed
reneweb wants to merge 1 commit into
react:masterfrom
reneweb:flatlist-native-props
Closed

[FlatList] Add setNativeProps#13529
reneweb wants to merge 1 commit into
react:masterfrom
reneweb:flatlist-native-props

Conversation

@reneweb

@renewebreneweb commented Apr 16, 2017

Copy link
Copy Markdown
Contributor

Motivation (required)

Curently FlatList does not implement setting native props directly like the old ListView did. This pr introduce the setNativeProps function which delegates to MetroListView or VirtualizedList. Thos don't have setNativeProps handling either, so, I delegated further to the respective ListView and Scroll components, which do have handling for it, thus, allowing to set the native props through FlatList.

Test Plan (required)

Create a project with a FlatList and change a native property using setNativeProps:

componentDidMount(){setInterval(()=>{this.list.setNativeProps({style: {backgroundColor:"white"}})},1000)}render(){return(<Viewstyle={styles.container}><FlatListref={component=>this.list=component}style={{backgroundColor:"black"}}data={[{key: 'a'},{key: 'b'}]}renderItem={({item})=><Text>{item.key}</Text>}/></View>)}

Fixes#13501

@facebook-github-botfacebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. GH Review: review-needed labels Apr 16, 2017
@ide

ide commented Apr 20, 2017

Copy link
Copy Markdown
Contributor

@sahrens this looks good to me, can you take a look?

@ide
ide requested a review from sahrensApril 20, 2017 21:15
@hramoshramos changed the title Add setNativeProps for flat list[FlatList] Add setNativePropsApr 25, 2017
@chitezh

Copy link
Copy Markdown

@sahrens@ide Please, what is the status of this PR?

@sahrenssahrens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks! Sorry I missed the earlier mention.

@facebook-github-botfacebook-github-bot added GH Review: accepted Import Started This pull request has been imported. This does not imply the PR has been approved. and removed GH Review: review-needed labels Jun 20, 2017
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sahrens has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

AdamPD pushed a commit to pharmadata/react-native that referenced this pull request Jul 20, 2017
Summary:
Curently FlatList does not implement setting native props directly like the old ListView did. This pr introduce the `setNativeProps` function which delegates to MetroListView or VirtualizedList. Thos don't have `setNativeProps` handling either, so, I delegated further to the respective ListView and Scroll components, which do have handling for it, thus, allowing to set the native props through FlatList.
Create a project with a FlatList and change a native property using `setNativeProps`:
```javascript
componentDidMount() {
setInterval(() => {
this.list.setNativeProps({ style: {backgroundColor:"white"} })
}, 1000)
}
render() {
return (
<View style={styles.container}>
<FlatList ref={component => this.list = component}
style={{backgroundColor:"black"}}
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <Text>{item.key}</Text>} />
</View>
)
}
```
Fixesreact#13501Closesreact#13529
Differential Revision: D5283593
Pulled By: sahrens
fbshipit-source-id: 8f96f88e286042d82452fef924689b5a8a783987
@technoplato

technoplato commented Sep 25, 2019

Copy link
Copy Markdown

Can we use setNativeProps with FlatList now? I'm currently unable on RN61.1

edit: I've got setNativeProps working with a subset of FlatList props

@sahrens@ide
I'm trying to use refs to set the onEndReached prop of my FlatList imperatively. Is there a way to do that?

I've modified an example from the PR that adds setNativeProps that toggles the color from black to white on an interval, but can't get onEndReached or onScroll to be called.

Can anyone help me understand what I'm doing wrong?

exportdefaultclassTestingextendsReact.Component{componentDidMount(){lettick=0this.list.setNativeProps({onEndReached: info=>{// NEVER CALLED 😢console.log('L231 on Scroll info ===',info)},onScroll: info=>{// NEVER CALLED 😢console.log('L250 info ===',info)},// Background DOES flash red on load... 🤔 style: {backgroundColor: 'red'}})setInterval(()=>{this.list.setNativeProps({onEndReached: info=>{console.log('L231 on Scroll info ===',info)},// Background DOES toggle black and white... 🤔 style: {backgroundColor: tick++&2 ? 'white' : 'black'}})},1000)}render(){return(<Viewstyle={styles.container}><FlatListref={component=>(this.list=component)}style={{backgroundColor: 'black'}}data={[{key: 'a'},{key: 'b'}]}renderItem={({ item })=><Text>{item.key}</Text>}/></View>)}}

@a-eid

Copy link
Copy Markdown

@technoplato why would u wanna setNativeProps for onEndReached or onScroll, you can provide them as normal props and if or when you wanna change the fn you can simply change the reference or change some sort of flag & check for it inside the function.

@technoplato

Copy link
Copy Markdown

@a-eid it’s been a little since I was working on this, but I think I was trying to create a library that would turn your list into one with infinite scroll capabilities. I’m sure there are better ways and that I was missing the forest through the trees.

@aminta

Copy link
Copy Markdown

So, in 2022 is it now possibile?

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.Import StartedThis pull request has been imported. This does not imply the PR has been approved.JavaScript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FlatList] setNativeProps does not work with FlatList component

10 participants

@reneweb@ide@chitezh@facebook-github-bot@technoplato@a-eid@aminta@sahrens@MattyK14@shergin