Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlatList] Add setNativeProps #13529

Closed
wants to merge 1 commit into from

Conversation

reneweb
Copy link
Contributor

@reneweb reneweb commented Apr 16, 2017

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 (
      <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>
    )
  }

Fixes #13501

@facebook-github-bot facebook-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
Copy link
Contributor

ide commented Apr 20, 2017

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

@ide ide requested a review from sahrens April 20, 2017 21:15
@hramos hramos changed the title Add setNativeProps for flat list [FlatList] Add setNativeProps Apr 25, 2017
@chitezh
Copy link

chitezh commented Jun 20, 2017

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

Copy link
Contributor

@sahrens sahrens left a comment

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-bot facebook-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
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>
    )
  }
```

Fixes facebook#13501
Closes facebook#13529

Differential Revision: D5283593

Pulled By: sahrens

fbshipit-source-id: 8f96f88e286042d82452fef924689b5a8a783987
@technoplato
Copy link

technoplato commented Sep 25, 2019

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?

export default class Testing extends React.Component {
  componentDidMount() {
    let tick = 0
    this.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 (
      <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>
    )
  }
}

@a-eid
Copy link

a-eid commented Sep 14, 2020

@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

@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

aminta commented Oct 6, 2022

So, in 2022 is it now possibile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This 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