3

I have implemented a tab view using React native, but I need to navigate from child screen to another parent screen.

See this image: My UI

In the above image when I click on the Next button, I am able to navigate between tabs, but I need to navigate to another main view. How can I do this?

Find the below code:

MainScreen.js

class MainTabView extends Component {
    state ={
      index: 0,
      routes: [
          { key: 'offers', title: 'Offers'},
          { key: 'ternding', title: 'Trending'},
          { key: 'retails', title: 'Retails'}
      ]
    }
    
    _updateRoute(newIdx) {
        this.setState({index: newIdx})
        }

    render() {
        return(
            <View style={styles.scene}>
                <Button title={'Main'} onPress={() => { this.props.navigation.navigate('Splash')}}></Button>
                 <TabView
                    navigationState={this.state}
                    renderScene={SceneMap({
                        offers: Offers,
                        ternding: Retails,
                        retails: Trending,
                    })}
                    onIndexChange={index => this.setState({ index })}
                    initialLayout={{ width: Dimensions.get('window').width }}
                    indicatorStyle={{ backgroundColor: 'pink' }}
                    useNativeDriver
                />  
            </View>
        )
    }
}

Offers.js

class Offers extends Component {

    state = {
        title: 'Next Screen'
    }
    componentDidMount() {

    }
    render() {
        return (
            <View style={styles.background}>
                <Button title={'Next'} onPress={() => {
                    this.props.navigation.navigate('Splash')
                }}></Button>
            </View>
        );
    }
}
Ravi Kumar
  • 31
  • 4
  • 1
    Hi Ravi, welcome to stackoverflow! You should take a look at [how to ask](https://stackoverflow.com/help/how-to-ask) a good question. In particular, it is almost always necessary to include source code in your question so we can see what you have tried to help get an answer. – robsiemb Oct 30 '19 at 14:56
  • Hi Robsiemb, i have updated my code. so please check and tell me that solution. – Ravi Kumar Nov 01 '19 at 11:09

1 Answers1

1

I came across this same issue and i was able to get the navigation object in children tabs using

import { withNavigation } from 'react-navigation';

https://reactnavigation.org/docs/en/with-navigation.html referenced from this issue https://github.com/react-native-community/react-native-tab-view/issues/907

Abass Ajanaku
  • 21
  • 1
  • 2