Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a component in all screens with React-Navigation - React Native

I want to have a component like button (or react-native-action-button precisely) to be shown in each screen using React Navigation.

Does React Navigation has any option in inside Routes to implement it once and use everywhere?

Note: I don't want to import Component in each .js class and use it in render.

Reference -> Like this in app's each screen. Floating Action Button Reference

like image 902
Harshit Saxena Avatar asked Mar 01 '18 14:03

Harshit Saxena


People also ask

How do I get the previous screen in react navigation?

Going back​The header provided by the native stack navigator will automatically include a back button when it is possible to go back from the active screen (if there is only one screen in the navigation stack, there is nothing that you can go back to, and so there is no back button).

What is stack screen?

Stack Navigator provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, use OS default animation on Android.


2 Answers

In your Navigation Container add it like this`

  <NavigationContainer>
    <Stack.Navigator initialRouteName="SplashScreen">
     ...
    </Stack.Navigator>
    
    <Button /> // add your component here. It will be visible in all screens
  </NavigationContainer>
like image 176
sandarshnaroju Avatar answered Nov 01 '22 19:11

sandarshnaroju


Make a default screen with that button. Everytime when you want to use that button wrap your all items with that component instead of view. For example 'DefaultScreen' is your view with the default component. Use it like that:

render(){
  <DefaulScreen>
    {AllOtherItems}
  </DefaulScreen>
}

And in that DefaultScreen render all children like this:

class DefaultScreen extends Component {
  render(){
    <View>
     {this.props.children}
     {defaultComponentThatYouWantToPutWithCustomStyle}
    </View>
  }
}
like image 1
VolkanSahin45 Avatar answered Nov 01 '22 19:11

VolkanSahin45