抽屉导航边栏部分

huangapple go评论53阅读模式
英文:

react navigation drawer sidebar section

问题

在这里,我正在尝试为平板电脑用户界面制作一个侧边栏,并尝试使侧边栏只在Drawer.Screen的名称为'A'时显示,但如果更改屏幕,则不在其屏幕上显示侧边栏。

以下是路由的代码:

const RoutesTablet = () => {

  return (
    <Div row flex={1}>
      <StatusBar backgroundColor={COLOR_PRIMARY} />
      <Drawer.Navigator
        initialRouteName="A"
        screenOptions={{
          headerStyle: {
            backgroundColor: COLOR_PRIMARY,
            // height: heightPercentageToDP(8),
          },
        }}
      >
        <Drawer.Screen
          name="A"
          component={Dashboard}
          options={{
            headerTitle: "Bob",
            headerRight: () => (
              <Image
                source={require("..........")}
                w={widthPercentageToDP(25)}
                h={heightPercentageToDP(4)}
                resizeMode="contain"
              />
            ),
          }}
        />
        <Drawer.Screen name="B" component={Sales} />
      </Drawer.Navigator>
      <SideBar />
    </Div>
  );
};

我已将代码部分翻译并保留了原始格式。如果有任何其他翻译需求,请告诉我。

英文:

in this im trying to make a sidebar for the tablet UI and i wan to try so that if the

Drawer.Screen name = 'A'

the sidebar going to show but if change screen the sidebar did not show on their screen

here is the code of the Routes

const RoutesTablet = () =&gt; {


return (
    &lt;Div row flex={1}&gt;
      &lt;StatusBar backgroundColor={COLOR_PRIMARY} /&gt;
      &lt;Drawer.Navigator
        initialRouteName=&quot;A&quot;
        screenOptions={{
          headerStyle: {
            backgroundColor: COLOR_PRIMARY,
            // height: heightPercentageToDP(8),
          },
        }}
      &gt;
        &lt;Drawer.Screen
          name=&quot;A&quot;
          component={Dashboard}
          options={{
            headerTitle: &quot;Bob&quot;,
            headerRight: () =&gt; (
              &lt;Image
                source={require(&quot;..........&quot;)}
                w={widthPercentageToDP(25)}
                h={heightPercentageToDP(4)}
                resizeMode=&quot;contain&quot;
              /&gt;
            ),
          }}
        /&gt;
        &lt;Drawer.Screen name=&quot;B&quot; component={Sales} /&gt;
      &lt;/Drawer.Navigator&gt;
      &lt;SideBar /&gt;
    &lt;/Div&gt;
  );
};

答案1

得分: 1

以下是代码的翻译部分:

A possible solution is to keep track of the state of NavigationContainer (link to doc) and update the current screen name. We only render SideBar if the current screen name is 'A'.

Demo

抽屉导航边栏部分

Source code

import * as React from 'react';
import {Text, View} from 'react-native';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {NavigationContainer} from '@react-navigation/native';

const A = () => <View />;
const B = () => <View />;

const SideBar = () => (
  <View
    style={{
      position: 'absolute',
      justifyContent: 'space around',
      right: 0,
      top: '25%',
      width: '10%',
      height: '50%',
      backgroundColor: 'lightblue',
    }}>
    {'sidebar'.split('').map(text => (
      <View key={text} style={{alignItems: 'center'}}>
        <Text>{text}</Text>
      </View>
    ))}
  </View>
);

const Drawer = createDrawerNavigator();

export default function App() {
  const ref = React.useRef(null);
  const [curScreen, setCurScreen] = React.useState('A');

  React.useEffect(() => {
    // Use this to constantly listen to the current state of
    // NavigationContainer and update curScreen
    const unsubscribe = ref.current.addListener('state', e => {
      const lastHist = e.data.state.history.slice(-1)[0];
      if (lastHist.type === 'drawer') {
        setCurScreen('drawer');
      } else {
        setCurScreen(ref.current.getCurrentRoute().name);
      }
    });
    return unsubscribe;
  });

  return (
    <NavigationContainer ref={ref}>
      <Drawer.Navigator initialRouteName="A">
        <Drawer.Screen name="A" component={A} />
        <Drawer.Screen name="B" component={B} />
      </Drawer.Navigator>
      {curScreen === 'A' && <SideBar />}
    </NavigationContainer>
  );
}

希望这对你有所帮助。如果你需要进一步的翻译,请告诉我。

英文:

A possible solution is to keep track of the state of NavigationContainer (link to doc) and update the current screen name. We only render SideBar if the current screen name is &#39;A&#39;.

Demo

抽屉导航边栏部分

Source code

import * as React from &#39;react&#39;;
import {Text, View} from &#39;react-native&#39;;
import {createDrawerNavigator} from &#39;@react-navigation/drawer&#39;;
import {NavigationContainer} from &#39;@react-navigation/native&#39;;

const A = () =&gt; &lt;View /&gt;;
const B = () =&gt; &lt;View /&gt;;

const SideBar = () =&gt; (
  &lt;View
    style={{
      position: &#39;absolute&#39;,
      justifyContent: &#39;space-around&#39;,
      right: 0,
      top: &#39;25%&#39;,
      width: &#39;10%&#39;,
      height: &#39;50%&#39;,
      backgroundColor: &#39;lightblue&#39;,
    }}&gt;
    {&#39;sidebar&#39;.split(&#39;&#39;).map(text =&gt; (
      &lt;View key={text} style={{alignItems: &#39;center&#39;}}&gt;
        &lt;Text&gt;{text}&lt;/Text&gt;
      &lt;/View&gt;
    ))}
  &lt;/View&gt;
);

const Drawer = createDrawerNavigator();

export default function App() {
  const ref = React.useRef(null);
  const [curScreen, setCurScreen] = React.useState(&#39;A&#39;);

  React.useEffect(() =&gt; {
    // Use this to constantly listen to the current state of
    // NavigationContainer and update curScreen
    const unsubscribe = ref.current.addListener(&#39;state&#39;, e =&gt; {
      const lastHist = e.data.state.history.slice(-1)[0];
      if (lastHist.type === &#39;drawer&#39;) {
        setCurScreen(&#39;drawer&#39;);
      } else {
        setCurScreen(ref.current.getCurrentRoute().name);
      }
    });
    return unsubscribe;
  });

  return (
    &lt;NavigationContainer ref={ref}&gt;
      &lt;Drawer.Navigator initialRouteName=&quot;A&quot;&gt;
        &lt;Drawer.Screen name=&quot;A&quot; component={A} /&gt;
        &lt;Drawer.Screen name=&quot;B&quot; component={B} /&gt;
      &lt;/Drawer.Navigator&gt;
      {curScreen === &#39;A&#39; &amp;&amp; &lt;SideBar /&gt;}
    &lt;/NavigationContainer&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月6日 11:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定