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.

this.props.navigate inside carousel is not working

See original GitHub issue

Hello, I want to add navigation to my carousel but its not working I keep getting this error cannot read property navigation of undefined I used the example provided in this repo, and I added my StackNavigator like this inside index.js

import { StackNavigator } from 'react-navigation';
import Feed from './Feed';
class Feeds extends Component...

export default StackNavigator({
  Feeds: {
    screen: Feeds,
    navigationOptions: {
       header: null
    },
  },
  Result: {
    screen: Feed
  }
})

This is my Feed screen that i want to navigate to it when the user choose an item inside the carousel


import React, { Component } from 'react';
import {
  ToastAndroid,View,Text,
} from 'react-native';
export default class Feed extends Component {
  constructor(props) {
    super(props);
    this.state = {
      feed: props.navigation.state.params.feed,
      report: null
    };
  }
  static navigationOptions = ({ navigation }) => {
    return {
      title: `Feed / ${navigation.state.params.feed}`,
    }
  };
  render() {
    <View>
        <Text>{this.navigation.state.params.feed}</Text>
    </View>
  }
}

and then inside SliderEntry.js I’ve just add a button to navigate into the Feed screen <Button onPress={() => this.props.navigation.navigate("Result", {feed: title})} /> this.props contain only

static propTypes = {
        title: PropTypes.string.isRequired,
        subtitle: PropTypes.string,
        illustration: PropTypes.string,
        even: PropTypes.bool,
    };

no navigation inside, am I doing it wrong ?

Issue Analytics

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

github_iconTop GitHub Comments

17reactions
jordangrantcommented, Nov 13, 2017

@efstathiosntonas @Pradnyana28 Here’s my solution:

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
          >
1reaction
trandat9hcommented, Nov 4, 2020

How to do this with fuctional components.

do you have any solutions?

Read more comments on GitHub >

github_iconTop Results From Across the Web

this.props.navigate inside carousel is not working #83 - GitHub
Hello, I want to add navigation to my carousel but its not working I keep getting this error cannot read property navigation of...
Read more >
I am facing the issue while trying to navigate to pages using ...
I am facing the issue while trying to navigate to pages using carousel ; import { StyleSheet ; Text, FlatList ; SafeAreaView, ScrollView ......
Read more >
React Bootstrap Carousel Component
Carousels don't automatically normalize slide dimensions. ... You can also control the Carousel state, via the activeIndex prop and onSelect handler.
Read more >
React navigation withNavigation HOC not working in carousel ...
Coding example for the question React navigation withNavigation HOC not working in carousel-React Native.
Read more >
react-material-ui-carousel - npm
A Generic, extendible Carousel UI component for React using Material UI. Latest version: 3.4.2, last published: 7 months ago.
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