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.

Best way to access Navigation prop with renderItem function

See original GitHub issue

Hi, this project looks awesome but I had a quick question about the new renderItem function.

I would like to pass the navigation prop down to my individual SliderEntry items, but can’t seem to find the best way to do so. I tried doing something like this, but the renderItem function in this example doesn’t seem to take any other arguments. I am working off of the example in this repo.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

38reactions
jordangrantcommented, Nov 13, 2017

@pcooney10 @lucienboillod Hey guys, here’s my solution for this:

In your Carousel:

<Carousel
    data={image1}
    renderItem={this._renderItem.bind(this)}   //<------
    sliderWidth={equalWidth2}
    itemWidth={equalWidth5}
  />

Adding the bind allows the _renderItem function to understand what “this” is (in this.props.navigation.)

In the renderItem function:

_renderItem ({item, index}) {
        return (
            <SliderEntry
              data={item}
              navigation={this.props.navigation}   //<-------
            />
        );
    }

And inside SliderEntry.js:

export default class SliderEntry extends Component {

    static propTypes = {
        data: PropTypes.object.isRequired,
    };

    render () {
        const { data: { title, subtitle, illustration}, navigation } = this.props;    //<------

        return (
          <TouchableOpacity
          activeOpacity={1}
          style={styles.slideInnerContainer}
          onPress={() => navigation.navigate('Feed')}  //<------- now you can use navigation
          >

Hope this helps!

2reactions
pcooney10commented, Dec 22, 2017

@koswarabilly Adapting the example above:

<Carousel
    data={image1}
    renderItem={this._renderItem}   //<------
    sliderWidth={equalWidth2}
    itemWidth={equalWidth5}
  />
_renderItem ({item, index}) {
        return (
            <SliderEntry
              data={item}
              index={index}
            />
        );
    }
import { withNavigation } from 'react-navigation'

class SliderEntry extends Component {

    static propTypes = {
        data: PropTypes.object.isRequired,
    };

    render () {
        const { data: { id, title, subtitle, illustration}, navigation } = this.props;    //<------

        return (
          <TouchableOpacity
          activeOpacity={1}
          style={styles.slideInnerContainer}
          onPress={() => navigation.navigate('Feed', {id: id})}  //<------- 
          >

export default withNavigation(SliderEntry)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Access the navigation prop from any component
Access the navigation prop from any component ; * as React from 'react'; ; import { Button } from 'react-native'; ; function GoToButton({...
Read more >
Passing Navigator object in renderItem - react native
Instead of passing the navigation prop, you can try using the withNavigation HOC. Where your List Component is defined:
Read more >
Passing Props to Another Screen Using React Navigation
Yo! In todays video we're PUSHIN' P!!!! and we're also gonna be learning how to work with navigation props. and pass anime ...
Read more >
SectionList - React Native
Rendered in between each item, but not at the top or bottom. By default, highlighted , section , and [leading/trailing][Item/Section] props are ...
Read more >
Getting Started with React Navigation v6 and TypeScript in ...
If you are using @react-navigation/stack , you can use StackScreenProps instead of StackNavigationProp . Adding type checks for route params. To ...
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