How to pre-render a Tab within BottomTabNavigator? - react-native

I'm using BottomTabNavigator for my React Native application. I've 4 tabs named TabA, TabB, TabC and TabD.
Basically, React Native render the tabs once they're focused however in my situation i would like to pre-render TabC even it's not focused jet so my question is how can i pre-render a Tab without focusing on it?

You have to set the lazy property to false.
By default, it will be set to true. So each tab will render only when appears on the screen.
You have to change it explicitly by
createBottomTabNavigator({
viewProfile: ViewProfileScreen,
calendar: CalendarScreen,
},
lazy: false
);

Related

Make all tab bar buttons unfocused on specific screens

I have a react native app which uses react navigation (V6.x for sure). My app has a main navigator which is a bottom-tabs navigator and contains three screens (tabs). Every one of these screens are stack navigators themselves. Let's say one of my tabs is named Wallet (others are Settings and Transactions). Inside this Wallet screen (which is a stack navigator), i have a HomePage screen, a Receive screen and a Send screen. I want to achieve the following behavior (like below screenshot from designs):
Whenever the user goes to one of Send or Receive screens, i want all the tab bar buttons become unfocused (tab bar is still visibe though). And whenever the user gets back to HomePage screen (or going to Settings or Transactions tab by pressing the corresponding tab button), I want the relevant tab button to get focused again. How can i achieve that with react navigation itself?
(My project is managed by redux, but i prefer not to use state management tools and use react navigation itself)
You can do that, but checking child navigation state inside your TabNavigator's screenOptions.
screenOptions={({ route, navigation }) => {
// get wallet stack route
const walletStack = navigation.getState().routes.find((route) => route.name === 'Wallet');
// get current wallet stack focused screen
const walletRouteName = getFocusedRouteNameFromRoute(walletStack);
const shouldBeUnfocused =
walletRouteName === 'Send' || walletRouteName === 'Receive';
{...}
}
Based on shouldBeUnfocused you can render proper icons and colors. Here is the snack with example code. You can red here about customizing tab bar's appearance.

Stack navigator inside Modal - React Native

I want to embed a stack navigator inside a Custom Modal component. Is there a way to do that with react navigation v4?
Basically I have a ModalCustomComponent, which is a filter Modal, and inside that I want to use a stack navigator to show different filter stages. I don't want to use the default react navigation modal mode but my own custom component only.
I don't know, is it possible to use stack navigator inside modal.
but for filter stages inside modal, I use this code
const [indexStage, setIndexStage] = useState(0)
const stages = [<FirstStage/>,<SecondStage/>,<ThirdStage/>]
return <Modal>{stages[indexStage]}</Modal>

How to prevent changing the screen when using "this.props.navigation.navigate"?

I'd like to pass the values from Tab to another Tab and I follow instructions from this tutorial https://reactnavigation.org/docs/en/params.html
I got the values at the end but i don't want to change the screen suddenly.
I tried to use 'setParams' instead but it doesn't work.
Setting tab (Selection) :
onPress={() => {
console.log('Select : '+ item.code);
this.props.navigation.navigate('A-page', {adCode: item.code});
}}
First tab (A-page) :
this.setState ({
AirportCode : this.props.navigation.getParam('adCode','----')
})
Any ideas for passing values across Tab navigation or method to stop changing the screen suddenly?
This looks more like a Redux use case then a React Navigation one. If you wanna change properties that influence more than one screen in your app, you should store these properties at the redux store.

Native Base and TabNavigator get active status in tabBarComponent

I am using Native Base and TabNavigator to display custom tabs.
How can I change the "active" property of the Button element to reflect the custom active tab?
Solved by adding:
active={props.navigation.state.index === 0}
Here is the logged value:
Also, you can go to state.routes to get the list of the screens.

Hide parent's navigation header from the nested navigator

I'm developing my first react native app. I've an issue with the nested navigations in the app.
I've the following navigations:
Main App Navigator : createStackNavigator
Authentication Navigator : createStackNavigator
Bottom Bar Navigator : createBottomTabNavigator
Top Tab Navigator : createMaterialTopTabNavigator
My too nested Navigator : createStackNavigator
What i want ?
I'm trying to hide the BottomBar & TopTab Navigators headers form a screen in the last nested navigator.
What I did?
Ive tried to set the header as null in my nested nav, but thats hides the nested header not the parents headers.
I also tried to set the parents headers as nulls, but thats hide them from all screen.
I need to only hide them in this nested screen. Can I change the parents headers property from my nested React Class?
Unfortunately, I didn't figure how to do that without using redux.
So I had to do a workaround.
I declared my Nested Navigator directly in the Main Navigator. "in the same level as Authentication & Bottom Bar Navigations" and set the header as null for this specific nav.
And then, navigate to that nested whenever i want.
Also, I had to add my custom icon to navigate the user back. because in our case there is no history in the new navigator in order to navigate back to.
so, i did like this:
static navigationOptions = ({ navigation }) => ({
headerLeft: (
<Icon
name="chevron-left"
color="#fff"
underlayColor="#4BA6F8"
onPress={() => {
const backAction = NavigationActions.back();
navigation.dispatch(backAction);
}}
/>
),
});
I know this is not the real answer for my question, but at least it solved my issue.