【问题标题】:How do I set custom drawer items to "focused" dynamically?如何将自定义抽屉项目动态设置为“聚焦”?
【发布时间】:2020-08-16 02:48:19
【问题描述】:

我的最终目标是根据其他地方设置的一些状态为一些抽屉项目提供自定义背景颜色。我知道为了给抽屉项目提供独特的背景颜色,我需要设置自定义抽屉内容。但是,我遇到了一个问题,我无法让自定义抽屉图标知道它们是否处于焦点。

我原本以为我可以只做一个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>
    );
}

【问题讨论】:

    标签: typescript react-native react-navigation react-native-paper


    【解决方案1】:

    没有 focused 可用于获取当前重点路线的道具。

    改为使用 DrawerNavigator 组件接收到的 props。查看this doc。

    如果您看到传递给drawerContent 组件的道具列表,您可以看到state 道具。来自文档,

    state - 导航器的导航状态,state.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];
       
    

    【讨论】:

    • 太好了。我可以稍微扩展一下这个推理,用focused={props.state.index === props.state.routes.findIndex(e =&gt; e.name === "Dashboard")} 设置抽屉项目效果很好!
    • 我做到了!谢谢你。不幸的是,因为我是新来的,所以我的投票不会公开显示。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多