0

I am trying to add a function to return button text based on the state inside the renderItem FlatList component.

renderButton() {
  return <Text>Button</Text>;
}

<TouchableWithoutFeedback>
    <TouchableOpacity>
        {this.renderButton()}
    </TouchableOpacity>
<TouchableWithoutFeedback>

This code return an error

function this.renderButton is not a function

Update:

<FlatList
      data={list}
      renderItem={this.renderRow}
    />

renderRow({item}) {
  return(
      <TouchableWithoutFeedback>
        <TouchableOpacity>
            {this.renderButton()}
        </TouchableOpacity>
      <TouchableWithoutFeedback>
  );
}

renderButton() {
  return <Text>Button</Text>;
}
5
  • Are you binding the renderButton function anywhere? Commented Aug 22, 2017 at 5:51
  • Need to see more code to be able to help Commented Aug 22, 2017 at 5:53
  • @Jayce444 not binding it anywhere else. only in this section of code. Commented Aug 22, 2017 at 6:08
  • @ShubhamKhatri I have updated my code. Commented Aug 22, 2017 at 6:11
  • 1
    You're not binding it anywhere in the code you've posted....are you binding it at all? Commented Aug 22, 2017 at 6:13

1 Answer 1

2

You need to bind the renderRow function to be able to access this.renderButton in it.

<FlatList
      data={list}
      renderItem={this.renderRow.bind(this)}
    />

or

<FlatList
      data={list}
      renderItem={() => this.renderRow()}
    />

or

renderRow = ({item}) => {
  return(
      <TouchableWithoutFeedback>
        <TouchableOpacity>
            {this.renderButton()}
        </TouchableOpacity>
      <TouchableWithoutFeedback>
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Got it working. Thank you. Understood to use of bind now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.