How to use navigation properly in React native - reactjs

I just started using React Native and at the moment I am trying to learn how to build a simple mobile application (testing on Andriod emulator). I got two screens/scenes at the moment (Home and Graph). this is what it should look like: header nav. design
I've been searching all over the internet, but can't seem to find the best way on how to implement this kind of header-navigation --> When pressed on the graph icon: the VIEW on the Home screen needs to swipe to the left (out of the screen) and the graphVIEW needs to come in from the right of the screen (like an animation).
I 've looked at react-navigation Tabs / Stacks, but they all seem to have fixed layouts etc.
Can someone help me with this or point me in the right direction?
Thank you very much!

I hope this is what your looking for...
Each screen has its own navigationOptions which helps to configure how its is presented.As of v1.0.0-beta.9 Navigation options can now be a function that receives same props that are available to the screen component.
Add navigationOptions inside your Home screen.
Now set Custom headerRight, your graph image wrapped inside some touchable item and hook the navigation.navigate to onPress as shown below.
static navigationOptions = ({ navigation, screenProps }) => ({
title: 'Home',
headerRight:(
<TouchableHighlight onPress={() => navigation.navigate('GraphScreen')} >
<Image source={require('./my-graph-icon.png')} />
</TouchableHighlight>
)
});
Note: The navigationOptions list are different for tab and stack navigators to which the screen is added to, the above should work for stack navigator.
Check React-Navigation docs for navigation options here

Related

React Native Navigations problem | Navigate One tab navigator screen to another

So basically I want to do navigate from one screen to another screen in React Native, but those screen are belongs different TabNavigator,
Let me explain structure
MainNavigator(createStackNavigator)
Drawer – CreateDrawerNavigation()
MainTabNavigator – CreateTabNavigator()
HomeTab
stackNavigator
Home - screen
MedicationTab
stackNavigator
Medications - screen
MedDetail - screen
so initially Home screen is loading right now, now I have bottom tab navigator to change tab navigator ,When I will go MedicationTab , It will loading Medications screen
But now I have one link on home page that open directly MedDetail screen, so basically i am moving one tab to another tab navigator , It's working fine and below is the snippet for navigation
navigation.navigate("MedicationsTab", {
screen: "MedDetails",
params: {
drug: drug,
}
});
Now from medDetail I am going back then it's opening HomeScreen again, Instead of medications screen , I will try CommomnActions.reset(), But it's not working
So anyone have any idea? How is this possible to do it in right way?

Can I add an animation to a React Native navigation button?

I have a button in react native, that I am using to navigate to another screen in my Drawer navigator (in addition to using the drawer screen itself). However, the transition is very abrupt. It it possible to have some kind of animation to make it smoother?
I have seen the docs for the transitioner, but these do not appear to be for a functional component like the screen the button is on. Is there any help?
Here is my button:
<TouchableOpacity
style={styles.buttonContainer}
onPress={() => this.props.navigation.navigate("SocialScreen")}>
<Text style={styles.buttonText}>Socials</Text>
</TouchableOpacity>
Any help is appreciated!
Of course you can add animation there.
I am attaching link of library with detail explanation react-native-navigation
Kindly comment if you face any error.

How can I achieve a component across every screen that can be updated by the current screen?

Hi I'm looking to have a modal (likely react-native-modalize) that is visible on every screen, but can adapt based on what screen is currently showing.
I don't want to introduce redux or anything like that. I don't think it's a tab navigation issue either, as this is a modal that starts out at only around 100 pixels but can open to almost full screen.
I have the modal as a component like this:
<QuickAccess {...props} />
And currently I call it on screens, but it's not an immediate render/doesn't seem to be a core part of the app as it just sort of... shows up.
Any thoughts would be really appreciated, thank you!
You can create a Modal and just render it on your root screen navigator like below and store its reference globally on your utility class and can show it anywhere in the app on any screen.
<Provider store={store}>
<MainScreen />
<YourModal ref={(ref) => Utility.setModalRef(ref)} />
</Provider>

How can I use Top Navigator and Bottom Navigator without having Top Navigator overlap with the Status Bar in React Navigation?

I am trying to nest a top navigator within a bottom navigator in react navigation. However when I do that, the top navigator collides with the status bar. I'm assuming it's because the bottom navigator is pushing it up. But how can I make it so both navigators show up, alongside the status bar? Below is an image of the overlapping issue. I'm using react navigation 5 and any other solution uses older versions which I've tried and don't work.
By the way I'm coding this on an iPhone 11 if that info is needed.
You should set your app container styles to have a marginTop value equal to the status bar height. Then your whole app will show below the status bar.
Something like this:
import Constants from "expo-constants";
...
const styles = StyleSheet.create({
container: {
marginTop: Constants.statusBarHeight
}
});

React-navigation tutorial missing back button

I am new to react native and I am following this tutorial from the official react-navigation docs on auth flow.
https://reactnavigation.org/docs/en/auth-flow.html
They have a really nice live example: https://snack.expo.io/#react-navigation/auth-flow-v3
After sign in, when you click on Show me more of the app from the home screen, it takes you to a page with a back button. Just from looking at the code, I can't figure out how they are able to produce this back button.
Maybe the code is not complete?
I understand that one way to a back button on the nav bar is to do something like:
headerLeft: (
<Button
onPress={() => alert('This is a button!')}
title="Info"
color="#fff"
/>
),
but this is missing from the code displayed?
React Navigation has by default provided Back button to navigate to the previous screen.
Because ios do not have a hardware back button all they have is a gesture with which they can go back. and if it is disabled then there is no way to go to the previous screen.
So default back button for stack navigator must be there and we have it.
So when there is a previous screen available back button will be there and if screen if first of its stack there will be no back button.
Let say we have one switch navigator for authentication screens like signIn, signUp etc. and one stack navigator for all other screens.
So here switch navigator by default has only one screen in its stack so none of these screens will have back button because you can't go back from here there is a dead end.
and for stack navigator, your first screen in the stack that renders before all other screens will not have a back button but all screens after that will be having back button because from there you can go back right.
React Navigation already have default Header with back button when you create switch or stack navigator. the headerLeft property is only used if you want to override the default back button. :D
Edit:
to clear things out.
The purpose of SwitchNavigator is to only ever show one screen at a time. By default, it does not handle back actions and it resets routes to their default state when you switch away. -switch navigator
where stack navigator purposely create screen on top of a stack (if you have initial screen). initial screen wont have header with back button because it doesn't know where return to. thats why when you navigate to "OtherStack" it creates new screen named "OtherStack" place on the top of "HomeScreen". newly created screens have default header with back button so you can go back to initial stack.
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });

Resources