gpt4 book ai didi

typescript - 如何将自定义抽屉项目动态设置为 "focused"?

转载 作者:行者123 更新时间:2023-12-03 13:59:33 25 4
gpt4 key购买 nike

我的最终目标是根据其他地方设置的几个状态为一些抽屉项目提供自定义背景颜色。我知道为了给抽屉项目提供独特的背景颜色,我需要设置自定义抽屉内容。但是,我遇到了一个问题,我无法让自定义抽屉图标知道它们是否处于焦点。
我原本以为我可以做一个 const [bHomeFocused, setbHomeFocused] = useState(false) (等)和设置状态 onPress然后设置 focused属性,但是当更多的抽屉元素进来时,我认为这听起来像是一个笨拙的解决方案。
我确定我缺少一个简单的答案,因为非自定义 DrawerItems 固有地具有此功能......

import { Drawer } from 'react-native-paper'
import { createDrawerNavigator, DrawerNavigationProp, DrawerItem, DrawerContentScrollView, DrawerItemList, DrawerContentComponentProps, DrawerContentOptions } from '@react-navigation/drawer';

function CustomDrawerContent(props: DrawerContentComponentProps<DrawerContentOptions>) {

return (
<DrawerContentScrollView {...props}>
<Drawer.Section>
<DrawerItem
label="Dashboard"
labelStyle={{ color: colorTheme.normalText }}
icon={() => <Icon name="book" type="feather" size={26} color={colorTheme.normalText} />}
activeBackgroundColor={colorTheme.panel}
inactiveBackgroundColor={colorTheme.cyan}
onPress={() => {
props.navigation.navigate('Dashboard')
}}
/>
</Drawer.Section>
<DrawerItem
label="Home"
labelStyle={{ color: colorTheme.normalText }}
icon={() => <Icon name="home" type="feather" size={26} color={colorTheme.normalText} />}
activeBackgroundColor={colorTheme.panel}
inactiveBackgroundColor={colorTheme.red}
onPress={() => {
props.navigation.navigate('HomeStack')
}}
/>
</DrawerContentScrollView>
);
}

export type DrawerParamList = {
Dashboard: undefined;
HomeStack: undefined;
};

export type DrawerProps<T extends keyof DrawerParamList> = {
navigation: DrawerNavigationProp<DrawerParamList, T>;
route: RouteProp<DrawerParamList, T>;
};

const AppDrawer = createDrawerNavigator<DrawerParamList>();

export default function MainDrawer({ route, navigation }: TopLevelStackProps<"MainDrawer">) {

return (
<AppDrawer.Navigator
drawerStyle={globalStyles.drawer}
drawerContent={
(props) => <CustomDrawerContent {...props} />
}
drawerContentOptions={{
labelStyle: { color: colorTheme.normalText },
activeBackgroundColor: colorTheme.panel,
inactiveBackgroundColor: colorTheme.background,
}}
>
<AppDrawer.Screen
name="Dashboard"
component={Dashboard}
options={{
unmountOnBlur: true,
}}
/>
<AppDrawer.Screen
name="HomeStack"
component={HomeStack}
options={{
unmountOnBlur: true,
}}
/>
</AppDrawer.Navigator>
);
}

最佳答案

没有 focused您可以使用 prop 获取当前的焦点路线。
相反,使用 DrawerNavigator 组件接收到的 Prop 。退房 this doc .
如果您看到传递给 drawerContent 的 Prop 列表组件,你可以看到 state支柱。从文档中,

state - The navigation state of the navigator, state.routes contains list of all routes


所以你可以得到路由数组和当前索引,它指示当前关注哪个屏幕。您可以获得 route name并将其与列表中的项目名称进行比较。
const {state} = props
const {routes, index} = state; //Not sure about the name of index property. Do check it out by logging the 'state' variable.
const focusedRoute = routes[index];

关于typescript - 如何将自定义抽屉项目动态设置为 "focused"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63432660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com