亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我嘗試在導航中使用道具時出現錯誤

當我嘗試在導航中使用道具時出現錯誤

侃侃無極 2023-10-14 17:05:18
我正在嘗試自定義反應本機導航,在使用道具選項時遇到一些問題這是我的 app.js 代碼    import { NavigationContainer } from '@react-navigation/native';import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'import { StatusBar } from 'expo-status-bar';import React from 'react';import { StyleSheet, Text, View } from 'react-native';import Home from './Screens/home';import Orders from './Screens/orders';import Account from './Screens/account';import TabComponent from './components/Tab'const Tab = createBottomTabNavigator()export default function App() {  return (         <NavigationContainer>        <Tab.Navigator>          <Tab.Screen  name="Home" component={Home} options={{            tabBarButton: (props) => <TabComponent label="home" {...props} />,          }} />          <Tab.Screen  name="My Orders" component={Orders} />          <Tab.Screen  name="Account" component={Account} />        </Tab.Navigator>      </NavigationContainer>     );}const styles = StyleSheet.create({  container: {    flex: 1,    backgroundColor: '#fff',     },});這是我的 tabs.js 代碼import React from 'react';import { TouchableWithoutFeedback } from 'react-native-gesture-handler';import styled from 'styled-components/native';import Images from '../images';const  Container = styled.TouchableWithoutFeedback``;const Background = styled.View``;const Icon = styled.Image``;const Label = styled.Text``;function Tab(label, accessibilityState ){    const active = accessibilityState.selected;    const icon = !active ? Images.icons[label] : Images.icons[ `${label}Active` ];    return(        <Container>            <Background>                <Icon source={icon}/>                <Label>{label}</Label>            </Background>        </Container>    );}export default Tab;
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

您在選項卡組件上錯誤地檢索道具,


下面的代碼可以幫助您了解如何傳遞 props。您可以解構您的 props 并將其傳遞到 jsx 內部,或者直接獲取 props 并使用 props.label(等)


function Tab({label, accessibilityState} ) //<== Destructed props.

{

        const active = accessibilityState.selected;

        const icon = !active ? Images.icons[label] : Images.icons[ `${label}Active` ];

        return(

            <Container>

                <Background>

                    <Icon source={icon}/>

                    <Label>{label}</Label>

                </Background>

            </Container>

        );

    }

    

    export default Tab;

Props 是一個單一對象,您可以在其中傳遞所有屬性。


另一種選擇是,


function Tab(props ) //<== props.

{

        const active = props.accessibilityState.selected;

        const icon = !active ? Images.icons[label] : Images.icons[ `${props.label}Active` ];

        return(

            <Container>

                <Background>

                    <Icon source={icon}/>

                    <Label>{props.label}</Label>

                </Background>

            </Container>

        );

    }


    export default Tab;


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號