如何使用React Native構建嵌套的抽屜菜單

by Dhruvdutt Jadhav

由Dhruvdutt Jadhav

如何使用React Native構建嵌套的抽屜菜單 (How to build a nested drawer menu with React Native)

Screen space is a precious commodity on mobile. The drawer menu (or “hamburger menu”) is one of the most popular navigation patterns that helps you save it while offering intuitive navigation. In this post, I will demystify how to build a nested (multi-level) drawer menu using React Native and React Navigation. ?

屏幕空間是移動設備上的寶貴商品。 抽屜菜單 (或“漢堡菜單”)是最受歡迎的導航模式之一,可在提供直觀導航的同時幫助您保存它。 在這篇文章中,我將揭開如何使用React Native和React Navigation構建嵌套(多級)抽屜菜單的神秘感。 ?

Try the live demo on mobile?or on web. ??

移動設備或WEB上嘗試現場演示。 ? ?

Navigation forms the backbone of a huge majority of apps built for production. The look and feel of navigation are important for driving use and engagement in mobile apps.

導航是為生產而構建的絕大多數應用程序的骨干。 導航的外觀對于推動移動應用的使用和參與度至關重要。

However, if you are React Native developer, there isn’t a clear opinion when it comes to building a navigation menu. React Native recommends a bunch of libraries for navigation. Each has its strength, depending on your needs, but there’s no one clear winner for all use-cases.

但是,如果您是React Native開發人員,則在構建導航菜單時并沒有明確的意見。 React Native 建議使用一堆庫進行導航。 每一種都有其優勢,取決于您的需求,但是對于所有用例而言,沒有一個明顯的贏家。

None of the navigation libraries currently support nested drawers out-of-the-box. But one of the libraries that provides a rich API to build custom solutions is React Navigation — a JavaScript-based navigation. It is strongly backed and maintained by the React Native community. This is what we’re going to use in this tutorial.

當前,沒有任何導航庫支持開箱即用的嵌套抽屜。 但是提供豐富的API來構建自定義解決方案的庫之一是React Navigation (基于JavaScript的導航)。 它由React Native社區大力支持和維護。 這就是本教程要使用的內容。

用例? (The use case ??)

I had to build a playground app to showcase a UI components library for React Native. It consists of eight different components, each supporting various props, and more than 50 different options.

我必須構建一個運動場應用程序來展示React Native的UI組件庫。 它由八個不同的組件組成,每個組件都支持各種道具以及50多種不同的選項。

It was not possible to show all options inside the drawer at one time without a multi-level drawer which would scope the options based on the selected component. I couldn’t find a ready-made solution for this, so I needed to build a custom one.

如果沒有一個多級抽屜將無法根據所選組件確定選項的范圍,則無法一次在抽屜中顯示所有選項。 我無法為此找到現成的解決方案,因此我需要構建一個定制的解決方案。

基本設置? (Base setup ?)

For the base setup, I’m assuming you already have a React Native project setup with either CRNA, Expo Kit, or React Native CLI. Make sure you have the react-navigation library installed with either yarn or npm. We’ll start right off with using the navigation API.

對于基本設置,我假設您已經具有使用CRNA , Expo Kit或React Native CLI的React Native項目設置。 確保您已安裝帶有yarn或npm的react-navigation庫。 我們將從使用導航API開始。

Feel free to check the getting-started guide before proceeding if you aren’t familiar with the React Navigation API.

如果您不熟悉React Navigation API,請在繼續之前先閱讀入門指南 。

We’ll start with an example similar to the one documented in the React Navigation’s DrawerNavigator official guide. We’ll create a simple drawer that has two drawer items: Home and Notifications.

我們將從一個類似于React Navigation的DrawerNavigator 官方指南中記錄的示例開始。 我們將創建一個具有兩個抽屜項目的簡單抽屜:“主頁”和“通知”。

自定義抽屜內容 (Custom drawer content)

React Navigation enables all navigators to do a lot of customizations by passing a navigator config as the second parameter. We’ll use it to render some custom content other than the stock drawer items.

通過將導航器配置作為第二個參數傳遞,React Navigation使所有導航器都可以進行很多自定義。 我們將使用它來呈現一些自定義內容,而不是庫存項目。

DrawerNavigator(RouteConfigs, DrawerNavigatorConfig)

DrawerNavigator (RouteConfigs, DrawerNavigatorConfig)

We’ll pass a prop called contentComponent to the config which would allow us to render custom content for the drawer. We’ll use that to show a header and footer along with the prevailing DrawerItems from react-navigation.

我們將一個名為contentComponent的道具contentComponent給配置,該道具將允許我們為抽屜渲染自定義內容。 我們將用它來顯示頁眉和頁腳以及來自react-navigation的流行DrawerItems

This potentially unlocks a lot of things that can be done by controlling what to render inside the drawer.

通過控制在抽屜內渲染的內容,這有可能解鎖很多事情。

創建屏幕映射 (Creating screen mapping)

We need to build a nested drawer for each component that we want to showcase. So let’s first register all the screens with the DrawerNavigator’s Config. We’ve created a separate screen mapping file for components. You can very well have your own convention, or define the object directly similar to the Home screen component.

我們需要為要展示的每個組件構建一個嵌套的抽屜。 因此,讓我們首先使用DrawerNavigator的Config注冊所有屏幕。 我們為組件創建了一個單獨的屏幕映射文件。 您可以很好地擁有自己的約定,或者直接類似于主屏幕組件定義對象。

The screen mapping consists of simple objects with screen property. The screenMapping object looks something like this:

屏幕映射由具有屏幕屬性的簡單對象組成。 screenMapping對象看起來像這樣:

After registering all components, the drawer would look something like this:

注冊所有組件后,抽屜將如下所示:

This would render all the components along with their options. We have two main components: DataSearch and TextField. Each has options like “With Icon Position,” “With Placeholder,” and more. Our task is to segregate these into a list of only components (DataSearch, TextField).

這將呈現所有組件及其選項。 我們有兩個主要組件: DataSearch和TextField 。 每個選項都有“帶有圖標位置”,“帶有占位符”等選項。 我們的任務是將它們分成僅包含組件(DataSearch,TextField)的列表。

分組外抽屜 (Grouping outer drawer)

A pattern I followed in the mapping was to use a delimiter _ to group together options from one component. For instance, the navigation keys I used were “DataSearch_Basic” and “DataSearch_With Icon Position”. This is exactly what is going to help us combine the options for a single component like DataSearch. We’ll evaluate uniquely all the components we need to show for the outer drawer.

我在映射中遵循的模式是使用定界符_將來自一個組件的選項組合在一起。 例如,我使用的導航鍵是“ DataSearch_Basic”和“ DataSearch_With Icon Position”。 這正是將幫助我們組合單個組件(如DataSearch)的選項的原因。 我們將唯一評估外部抽屜所需顯示的所有組件。

We’ll create a util function to evaluate outer drawer list items to render.

我們將創建一個util函數來評估要渲染的外部抽屜列表項。

This function will return an object with unique components for the main components like (DataSearch, TextField) that we’ll render on the screen with the help of the contentComponent custom component. We’ll also maintain a boolean to determine the content rendered on the drawer at a particular instant.

此函數將返回一個具有唯一組件的對象,這些組件具有主要組件(例如DataSearch,TextField),這些組件將contentComponent自定義組件在屏幕上呈現。 我們還將維護一個布爾值,以確定在特定時刻渲染在抽屜上的內容。

The renderMainDrawerComponent is just a function iterating over the keys of the components object. It renders custom outer drawer items built on top of simply Text and View from react-native. Check the full code here.

renderMainDrawerComponent只是一個在components對象的鍵上進行迭代的函數。 它呈現了基于react-native的簡單TextView之上的自定義外部抽屜項目。 在此處檢查完整代碼。

This will render the drawer like this:

這將使抽屜如下所示:

渲染兒童抽屜? (Rendering the child drawer ?)

Now, we need to show the options based on the component that is tapped. You might have noticed that in utils, we’re also extracting the start and end indexes of the component groups based on the delimiter pattern.

現在,我們需要根據所點擊的組件顯示選項。 您可能已經注意到,在utils中,我們還基于定界符模式提取了組件組的開始索引和結束索引。

For instance, DataSearch screens start at index 1 (index 0 is Home screen) and ends at 3. TextField starts at 3 and end at 5. We’ll use these indices to magically slice the items that are passed to DrawerItems based on the selected component and its indices.

例如,DataSearch屏幕從索引1開始(索引0是主屏幕),結束于3。TextField從3開始,結束于5。我們將使用這些索引根據所選內容神奇地切片傳遞給DrawerItemsitems組件及其索引。

Now, after tapping on DataSearch, the drawer will yield into something like this:

現在,在點擊DataSearch之后,抽屜將產生如下所示的內容:

We’ve also added a sweet back button which basically toggles a boolean to render the main drawer items. You can check the full code here.

我們還添加了一個漂亮的后退按鈕,該按鈕基本上切換了一個布爾值以呈現主抽屜項目。 您可以在此處查看完整的代碼。

Now, the only thing left to do is make the drawer items look cleaner by trimming the redundant component name. Again, the rich React Navigation API comes handy here.

現在,唯一要做的就是通過修剪冗余組件名稱來使抽屜項目看起來更整潔。 同樣,豐富的React Navigation API在這里很方便。

There are various properties we can pass with navigationOptions. A particular one we’re going to use here is the title prop with the screen mapping. This will let us remove the part before the first delimiter. So, “DataSearch_Basic” will show as “Basic” only.

我們可以通過navigationOptions傳遞各種屬性。 我們將在此處使用的一個特殊title是帶有屏幕映射的title道具。 這將使我們刪除第一個定界符之前的部分。 因此,“ DataSearch_Basic”將僅顯示為“ Basic”。

That’s all. We can add as many items we want based on the delimiter pattern. The playground app we’ve built consists of eight main components and over 53 total options.

就這樣。 我們可以基于定界符模式添加任意數量的項。 我們構建的游樂場應用程序由八個主要組件組成,共有53個選項。

Here’s the link to the final app and the codebase.

這是最終應用程序和代碼庫的鏈接 。

總結? (Summary ?)

  • Base setup: DrawerNavigation hello world from docs.

    基本設置 :從DrawerNavigation的hello world 文檔 。

  • Custom drawer content: Render drawer items with contentComponent.

    自定義抽屜內容 :使用contentComponent渲染抽屜項目。

  • Screen mapping: Define and register all drawers components.

    屏幕映射 : 定義并注冊所有抽屜組件。

  • Group outer drawer: Read delimiter pattern to group drawer items.

    分組外部抽屜 : 讀取定界符樣式以分組抽屜項目。

  • Rendering child drawer: Slice and render child drawer items.

    渲染兒童抽屜 : 切片并渲染兒童抽屜項目。

結論 (Conclusion ?)

We learned to build a multi-level drawer menu with React Native. We used React Navigation API to render a custom content component inside the drawer, and used the delimiter pattern for screen mapping. Use this pattern to build any level of nesting or conditional rendering for drawers.

我們學會了使用React Native構建多級抽屜菜單。 我們使用React Navigation API在抽屜內部渲染自定義內容組件,并使用定界符模式進行屏幕映射。 使用此模式可以為抽屜構建任何級別的嵌套或條件渲染。

ReactiveSearch? (ReactiveSearch ?)

Provides UI components for Native and Web platform to build perfect search experiences. You can check all the components it offers by playing with the playground app itself or by creating your own component.

提供用于本機和Web平臺的UI組件,以構建完美的搜索體驗。 您可以通過使用Playground應用本身或創建自己的組件來檢查其提供的所有組件 。

appbaseio/reactivesearchreactivesearch - A React and React Native UI components library for building data-driven appsgithub.com

appbaseio / reactivesearch reactsearch- 一個React和React Native UI組件庫,用于構建數據驅動的應用程序 github.com

翻譯自: https://www.freecodecamp.org/news/how-to-build-a-nested-drawer-menu-with-react-native-a1c2fdcab6c9/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394392.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394392.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394392.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c# WebApi之接口返回類型詳解

c# WebApi之接口返回類型詳解 https://blog.csdn.net/lwpoor123/article/details/78644998 轉載于:https://www.cnblogs.com/hwubin5/p/10665006.html

第十一次作業

1。題目&#xff1a; 輸入一個字符串&#xff0c;統計大寫字母、小寫字母、空格、數字和其他字符的個數。(要求用字符數組 代碼 #include<stdio.h> #define n 100 int main() {char a[n];int i,a10,b0,c0,d0;printf("輸入字符串&#xff1a;\n");gets(a);for(i…

Python Configparser模塊讀取、寫入配置文件

寫代碼中需要用到讀取配置&#xff0c;最近在寫python&#xff0c;記錄一下。 如下&#xff0c;假設有這樣的配置。 [db] db_host127.0.0.1 db_port3306 db_userroot db_pass [concurrent] thread200 processor400 可以使用ConfigParser模塊來讀取、寫入配置…

leetcode714. 買賣股票的最佳時機含手續費(動態規劃)

給定一個整數數組 prices&#xff0c;其中第 i 個元素代表了第 i 天的股票價格 &#xff1b;非負整數 fee 代表了交易股票的手續費用。 你可以無限次地完成交易&#xff0c;但是你每筆交易都需要付手續費。如果你已經購買了一個股票&#xff0c;在賣出它之前你就不能再繼續購買…

寧宛 機器人_全文閱讀 .007 忠犬機器人

全文閱讀 .007 忠犬機器人”其實光看i5高大的身軀、泛著金屬光澤的外殼&#xff0c;很難想象它能把照顧人的事情做的那么細致。這張同樣自帶程序的金屬床在i5的操作下&#xff0c;根據寧宛自身的體重及骨密度&#xff0c;調整出最適合她的硬度、角度及凹陷程度。空間跳躍……早…

servlet中文亂碼_10分鐘快速掌握Servlet相關基礎知識

Servlet的學習路線1、 創建Servlet2、 Servlet的相關配置3、 Servlet的生命周期4、 HttpServletRequest接口5、 HttpServletResponse接口6、 HttpSession接口7、 Filter、Listener接口Servlet的相關配置1、 創建Servlet extends HttpServlet2、 配置Serlvet第1種配置方式: web.…

蓋茨比喬布斯_如何使用蓋茨比創建您的博客并通過手機進行處理

蓋茨比喬布斯by Hu Chen胡Hu 如何使用蓋茨比創建您的博客并通過手機進行處理 (How to use Gatsby to create your blog and work on it from your phone) Recently, I decided to migrate my blog to Gatsby. Gatsby is a blazing fast static site generator based on React.…

python之collections之有序字典(OrderedDict)

一、定義OrderedDict是對字典的補充&#xff0c;它記住了字典元素的添加順序。eg&#xff1a; 二、OrderedDict相關方法def clear(self): # real signature unknown; restored from __doc__ """     od.clear() -> None. Remove all items from od. …

進階4:hive 安裝

安裝包&#xff1a; apache-hive-2.1.1-bin.tar.gz 安裝步驟&#xff1a; 1.上傳 apache-hive-2.1.1-bin.tar.gz 到linux; 2.解壓文件&#xff1a; tar zxvf apache-hive-2.1.1-bin.tar.gz 3.安裝mysql (僅支持mysql 5.7以下版本&#xff0c;不支持5.7或更高版本&#xff0c…

macbookpro接口叫什么_【科普】什么是雷電接口?蘋果電腦MACBOOK PRO有嗎?

剛接觸筆記本的朋友不知道USB-C口是什么,也不知道雷電接口(Thunderbolt)是什么,只知道MACBOOK PRO有雷電3接口。簡單來說 雷電接口是USB TYPE-C的替代模式,在此了解【什么是USB TYPE-C】 什么是雷電接口? 借用百度百科的表達 2011年2月24日,英特爾發布了長期以來廣為宣傳的…

GoldenGate 12.3微服務架構與傳統架構的區別

隨著Oracle GoldenGate 12c&#xff08;12.3.0.1.0&#xff09;的發布&#xff0c;引入了可用于復制業務數據的新架構。 多年來&#xff0c;這種架構有著不同的稱謂&#xff0c;Oracle終于在最后GA發布的版本中&#xff0c;以“Microservices”的名義確認新架構的名稱。Microse…

leetcode劍指 Offer 63. 股票的最大利潤(動態規劃)

假設把某股票的價格按照時間先后順序存儲在數組中&#xff0c;請問買賣該股票一次可能獲得的最大利潤是多少&#xff1f; 示例 1: 輸入: [7,1,5,3,6,4] 輸出: 5 解釋: 在第 2 天&#xff08;股票價格 1&#xff09;的時候買入&#xff0c;在第 5 天&#xff08;股票價格 6&…

usb serial port 驅動_tty初探 — uart驅動框架分析

寫在前面&#xff1a;我們沒有講UART驅動&#xff0c;不過我們認為&#xff0c;只要系統學習了第2期&#xff0c;應該具備分析UART驅動的能力&#xff0c;小編做答疑幾年以來&#xff0c;陸陸續續有不少人問到UART驅動怎么寫&#xff0c;所以今天就分享一篇深度長文(17000字&am…

databricks_如何開始使用Databricks

databricksby Shubhi Asthana通過Shubhi Asthana 如何開始使用Databricks (How to get started with Databricks) When I started learning Spark with Pyspark, I came across the Databricks platform and explored it. This platform made it easy to setup an environment…

簡述isodata算法的原理_算法常見面試題匯總(一):概率論與數理統計部分

初級或中級算法崗面試題主要有四類&#xff1a;數理統計基礎、機器學習模型原理、編程能力、項目經驗。項目經驗因人而異&#xff0c;所以僅總結前三個方面的基礎知識&#xff0c;分享給朋友。&#xff08;高級或資深算法崗面試內容不在本文范圍內&#xff09;1.大數定律弱大數…

shell中各種括號的作用()、(())、[]、[[]]、{}

轉自&#xff1a;http://blog.csdn.net/taiyang1987912/article/details/39551385 一、小括號&#xff0c;圓括號&#xff08;&#xff09; 1、單小括號 () ①命令組。括號中的命令將會新開一個子shell順序執行&#xff0c;所以括號中的變量不能夠被腳本余下的部分使用。括號中…

SQLite 數據類型

SQLite 數據類型 參考&#xff1a; SQLite 數據類型 | 菜鳥教程http://www.runoob.com/sqlite/sqlite-data-types.html SQLite 數據類型是一個用來指定任何對象的數據類型的屬性。SQLite 中的每一列&#xff0c;每個變量和表達式都有相關的數據類型。 您可以在創建表的同時使用…

leetcode1143. 最長公共子序列(動態規劃)

給定兩個字符串 text1 和 text2&#xff0c;返回這兩個字符串的最長公共子序列的長度。 一個字符串的 子序列 是指這樣一個新的字符串&#xff1a;它是由原字符串在不改變字符的相對順序的情況下刪除某些字符&#xff08;也可以不刪除任何字符&#xff09;后組成的新字符串。 …

php開發支付寶支付密碼忘記了怎么辦_密碼箱忘記密碼怎么辦?密碼箱解鎖方法大全...

密碼箱忘記密碼經常發生&#xff0c;有時候急著趕車趕飛機必須用的證件在密碼行李箱&#xff0c;怎么辦&#xff1f;破壞&#xff1f;當你忘記密碼的時候千萬不要著急&#xff0c;不要試著用暴力破壞密碼鎖。操作方法一此類型的密碼箱的開鎖方法。把箱子放在光線好的地方放平&a…

Python網絡編程之TCP服務器客戶端(二)

傳輸控制協議(官方術語為TCP/IP協議)是互聯網的重要組成部分。TCP的第一個版本是在1974年定義的&#xff0c;它建立在網際層協議(IP)提供的數據包傳輸技術之上。TCP使得應用程序可以使用連續的數據流進行相互通信&#xff0c;除非出現網絡原因導致連接中斷等意外情況&#xff0…