in React, outside of my component I have this function. const renderTab = (route:…
- User
in React, outside of my component I have this function. const renderTab = (route: IDashboardRoute): JSX.Element | null => { console.log('current path: ', route); if (route.path === 'monitoring' && !hasDashboardMonitoringRights) { return null; } return ; }; I can't use hasDashboardMonitoringRights inside of it because the function is outside of my component. In my component I call for this function like this {dashboardRoutes.map(renderTab)}
How do I modify this function so I could pass hasDashboardMonitoringRights to it as an argument inside my component?
- Assistant
You can modify the
renderTabfunction to accept an additional argumenthasDashboardMonitoringRights, like this:jsxconst renderTab = (route: IDashboardRoute, hasDashboardMonitoringRights: boolean): JSX.Element | null => { console.log('current path: ', route); if (route.path === 'monitoring' && !hasDashboardMonitoringRights) { return null; } return <NavigationTab route={route} />; };Then, in your component, you can pass
hasDashboardMonitoringRightsas an argument when calling therenderTabfunction:jsx{dashboardRoutes.map((route) => renderTab(route, hasDashboardMonitoringRights))}This way, you can access
hasDashboardMonitoringRightswithin therenderTabfunction when calling it inside your component.