I am making a React Native application in which I have menus and submenus.
Menus and submenus structure:
let arr = [
{
name: 'Header 1',
routeName: 'Home',
child: [
{ name: 'Header 1 - Submenu 1', child: [], routeName: 'Home' },
{
name: 'Header 1 - Submenu 2',
child: [],
routeName: 'NotificationScreen',
},
{ name: 'Header 1 - Submenu 3', child: [], routeName: 'Home' },
],
},
{
name: 'Header 2',
routeName: 'NotificationScreen',
child: [
{
name: 'Header 2 - Submenu 1',
child: [],
routeName: 'NotificationScreen',
},
{ name: 'Header 2 - Submenu 2', child: [], routeName: 'Home' },
{
name: 'Header 2 - Submenu 3',
child: [],
routeName: 'NotificationScreen',
},
],
},
];
Render of Menu’s and Submenu’s:
<TouchableOpacity style={styles.item} onPress={onPress} activeOpacity={1}>
<View style={styles.row}>
<Text style={{ paddingRight: 20 }}>{name}</Text>
{child.length ? <Text>{open ? 'close' : 'open'}</Text> : null}
</View>
{open &&
child.map((x: any, i: any) => {
if (x.child.length) {
return (
<Item
key={x}
active={childActive}
i={i}
setActive={setChildActive}
child={x.child}
/>
);
}
return (
<TouchableOpacity
key={x}
style={styles.subItem}
onPress={() => {
handleRouteChange(x.routeName);
}}>
<Text>
{name} - submenu - {i + 1}
</Text>
</TouchableOpacity>
);
})}
</TouchableOpacity>
Working example: https://snack.expo.dev/@manirajmurugan/custom-header-title-component
Here I am in the need to make a breadcrumb like structure on the screen page as per the navigation done by user through menu and submenus.
Current Scenario:
-> If user clicks on Header 1 and then select submenu Header 1 – Submenu 1, then the user will be redirected to Home Screen.
Expected Scenario:
-> Requirement here is that I am in the need to display a breadcrumb for this screen like,
Header 1 > Header 1 - Submenu 1
On click of the Header 1 in this breadcrumb, user will be redirected to the respective routeName
given in the object.
Kindly help me to generate breadcrumb for the navigation done in menu for the respective screen’s.
Thanks in advance.
Edit:
In real app menu and submenu will be like,
Here if user click’s on Stored Data configuration
under Unit Data Management
,
then the expected breadcrumb result would be,
2
Answers
For readability, I started by moving all of the components into their own separate files. I also used a FlatList instead of a ScrollView for the recursive SubMenuItem rendering.
I first broke the breadcrumb down into three components. The first,
MenuItem
, which would always be visible in the Header. When pressed, it would reveals a dropdown menu, which would reveal theSubMenu
, MenuItem’s children. PressingSubMenu
would reveal its children, if it had any, or would trigger navigation, if a routeName was provided. DemoCreate a piece of state to hold the crumbs. It will be an array that will hold three values: the screen you are currently on, the section title press, and the section item pressed. Handling the last two will be done by the SectionList and to keep track of the current screen you can add a screenListener to the StackNavigator that will listen for all navigation changes on all screens.
Now that current screen is handled, you can move on to deal with getting the SectionTitle, which the SectionHeader and MenuItem components handles. If a section data array’s length is zero, pressing it will trigger the parent component’s onItemPress with the section as an argument, if not, then pressing it will reveal its children
The SectionItem is pretty straightforward. When visible, its a button that when pressed will trigger its grandparent’s onItemPressed with both the section title and the item’s title
ButtonWithMenu contains a button that when pressed, renders the SectionList directly beneath it. It accepts an onItemPress prop which will be passed on into the SectionList:
Now in the Header component the only thing that is left to do is create a list for the SectionList to render and then update the crumb state with onItemPress:
Demo