question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FlatList's ListFooterComponent doesn't render arrow function with refs

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment:
  OS:  Linux 4.8
  Node:  8.9.0
  Yarn:  1.3.2
  npm:  5.6.0
  Watchman:  4.9.0
  react-native: 0.48.4
  react: 16.0.0-alpha.12

Steps to Reproduce

Create a FlatList with any items in content and a footer element with refs:

export default class MyElement extends Component {
    constructor(props) {
        super(props)
        this.state = { someData: [{id: 1, content: 'foo'}, {id: 2, content: 'bar'}], myInputState: '' }
    }
    _renderItem = ({item}) => <Text>{item.content}</Text>

    _renderFooter = () => {
        return (
            <TextInput ref='myReferencedInput' value={this.state.myInputState} onChangeText={(text) => this.setState({myInputState: text})} />
        )
    }

    render () {
        return (
            <FlatList
                data={this.state.somedata}
                renderItem={this._renderItem}
                keyExtractor={(item) =>  item.id}
                ListFooterComponent={this._renderFooter}
            />
        )
    }
}

Expected Behavior

The footer content get’s rendered with no problems, as it would happen in ListView

Actual Behavior

“Stateless functions cannot have refs” error.

If I call this._renderFooter() just below the FlatList, it works, but is not what I want, I want it as a footer.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
maieonbrixcommented, Jan 24, 2018

I just tried it and it worked, here is my working example

class Example extends Component {
 _renderFooter = () => {
   return <TextInput ref={node => this.t = node} />
}
  render() {
        return <FlatList 
                ListFooterComponent={this._renderFooter}
       />

here is the console.log(this)

screen shot 2018-01-24 at 17 12 15
0reactions
luizguilhermefrcommented, Jan 30, 2018

It works. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native FlatList BUG? - Stack Overflow
Another Option will be to use arrow functions (Better) renderFooter = () =>{ return(<View/>); }. and then ListFooterComponent={this.
Read more >
A deep dive into React Native FlatList - LogRocket Blog
FlatList is a React Native component that allows you to render lists with zero hassle and minimal code. Learn how to customize FlatList....
Read more >
8 ways to optimize React native FlatList performance
1. Avoid arrow functions inline for renderItem ... Don't use arrow functions inline for renderItem of the FlatList. It's better if you avoid...
Read more >
When should I use arrow functions with React?
Notice how constant doesn't change, while onChange does. This is because each call to render creates a new arrow function! Editor Preview.
Read more >
How to use the FlatList Component — React Native Basics
First create a new function that renders the component you want in the header. You then want to actually render that header by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found