Using React-Native navigation but not stack?
P粉141911244
P粉141911244 2023-08-17 18:26:37
0
1
303
<p>So I have been working on React-Native recently and have made great progress. I know how to use React-Navigation, whether it's <code>@react-navigation/native-stack</code> or <code>@react-navigation/bottom-tabs</code>. There are no problems when using these. </p> <p>But to better understand the tools I use, I have a question: </p> <p>When using the Navigator as intended, you end up with multiple screens that are "stacked" on top of each other, or are pulled apart when returning to the previous screen. I often look to Twitter for design inspiration, and I couldn't help but notice that their web application doesn't use this "stacked" approach. </p> <p>For the sake of simplicity, I'm only talking about the web side here, but it's basically the same on iOS, the screens stack. (Obviously I can't see if the Twitter app doesn't stack like the web app does, but whatever). </p> <p><strong>When using the Navigator in the correct manner as documented</strong></p> <pre class="brush:php;toolbar:false;"><div style="z-index: 0; display: flex;"> //Screen content </div> <div style="z-index: -1; display: none;"> //Contents of screen 2 </div> <div style="z-index: -1; display: none;"> //Contents of screen 3 </div></pre> <p>So, all screen components are technically rendered, only the selected screen is not hidden. </p> <p>What I want to do is always render only one screen, no other screens are hidden. As the user "navigates" to a different screen, I want the content of this one screen to change depending on which page they are going to. </p> <p>The way Twitter works is they have a <code><main></main></code> container which contains the content of the page and depending on which page the user is on, changes the container accordingly Content. Twitter does use React-Native, so there must be a way to do this. I wish there was at least a semi-official way to do this, rather than a de facto way to do it. I've searched in the documentation of 'React-Navigation' but found no similar description. </p> <p><strong>In summary: </strong>React-Navigation "stacks" screens so that they exist simultaneously, but only the screen "on top" of the stack is displayed. I'm looking for a way to use only one screen and have that screen's content change based on the page the user navigates to. </p> <p>I've searched through the documentation for React-Navigation, trying to find a description of the behavior similar to what I'm looking for, but haven't found anything. If there's a better React navigation library that does more of what I've described, I'd love to point it out. </p>
P粉141911244
P粉141911244

reply all(1)
P粉788765679

What you want to do is more suited to a single page application architecture where you have a single main container that holds changing content based on navigation, however, React Navigation is designed for stacking screens and navigating between them of.

solution

You can create a custom navigation component, let's call it NavigationContainer, which holds the changing content. This component will manage the currently displayed page and render the content accordingly, similar to this:

const NavigationContainer = () => {
  const [currentPage, setCurrentPage] = useState('Home'); // 默认页面

  const navigateTo = (page) => {
    setCurrentPage(page);
  };

  const renderContent = () => {
    switch (currentPage) {
      case 'Home':
        return ;
      case 'Profile':
        return ;
      // 添加更多不同页面的情况
      default:
        return null;
    }
  };

  return (
    
      
      {renderContent()}
    
  );
};

export default NavigationContainer;

CustomNavBar is another component that contains buttons or links for each page.
When the button/link is pressed it will call the navigateTo function which is received as props from its parent component i.e. NavigationContainer to update the currentPage# there ##state:

const CustomNavBar = ({ navigateTo }) => {
  return (
    
       navigateTo('Home')}>
        Home
      
       navigateTo('Profile')}>
        Profile
      
      // 添加更多按钮/链接以供其他页面使用
    
  );
};

export default CustomNavBar;
limit

You should implement deep linking for better URL-based navigation, which will allow users to navigate directly to a specific page using a URL. Additionally, you will need to manually handle routing, animations, and transitions if needed.

You may also lose some of the built-in navigation functionality provided by libraries like React Navigation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!