React native native hybrid routing solution

InfoQ 2021-06-18 04:30:56
react native native hybrid routing


{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" stay RN Many companies already have a complete set of App, Maybe the business is complicated 、 Dependence is numerous , under these circumstances , The original App It is obviously impractical to overturn rewriting , Cost and risk are high . So how to carry out mixed development is very important ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" at present RN The official proposed routing framework is react-navigation, Most of the logic of this framework is javascript Compiling ,RN The stack management of the page is done by JS End control , So when the original and RN Page mix development , Because of the native stack and RN Stack inconsistency , This leads to the confusion of stack management . appear RN - Native - Native - RN When you jump to the scene , If it is single RN Container management , You need to judge that the previous page is RN Still original , Embedded base class modification ; If more RN Container management , You also need to distinguish whether to jump or RN."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In addition, it is widely used on the Internet react-native-navigation frame , It USES iOS and Android The original foundation Api. But the problem of hybrid development is still with react-navigation Agreement ,JS Self managed stack and native stack are not unified , This leads to the complexity of stack management , Jumping requires extra processing . So through the analysis of some routes in the market , We pass the right react-native-navigation The transformation of the frame , Abandoned JS Stack management at the end , Will a RN The page corresponds to a container , bring RN Stack management is integrated into native stack management ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"WYNavigation"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Main concepts "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" We use a single engine multi container model . In the early stage, we have made an analysis of the existing RN The code was unpacked , Divided into one common Bao He n individual business package . And every page in the native project is based on RouteUrl( Similar to browser address ) The way to jump . These two points provide technical support for our scheme . Share one common Packages enable our code to share a RN engine ,RouteUrl We don't need to care about whether to jump to native or RN page . Here's how to open a RN The flow of the page : and react-native-navigation equally , We'll take all the RN Page in Navigation To register ,Test For this RN The external name of the page ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Navigation.registerComponent(\"Test\", () => TestScreen)\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" When the original main project starts, it will be preloaded first common package , When you need to open a RN When the page is , I'll open one first RN Containers , According to what was passed on PageName Find the corresponding business Package to load , then PageName to RN End ,RN End according to PageName Find the corresponding RN Page shows , The container is based on RN Static variables in the page initialize the native navigation bar , That's because it's time to RN A container is equivalent to a native page , There is only one in this container RN page , So a RN A page is a native container , So that the RN The page is embedded in the native navigation stack . That's it RN And the unification of native navigation stack ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/00\/00a7b66cae62acc83d7f237a73193a84.webp","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Components 、 page 、 Container understanding "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Here we are going to say 3 A concept : Components (Component):RN A unit with independent functions on a computer , A page consists of multiple components . page (Screen):UI Pages in a sense , Including navigation bar and content display . Containers (RNContainer): It's used to hold RN The container of , Used for display RN page . Our solution is multiple components = A page = A container = A native page , all RN On the page push and pop In fact, it will eventually go through the native jump of the container , This treatment is equivalent to RN There is no difference between jumping and returning pages and native pages ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/81\/81112c12c47a5dcb2a953d361f39f054.webp","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Jump of route "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Combined with what we have App Routing mode , We are united iOS and Android Protocol for containers at both ends , take RN The page name is passed in the form of parameters ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":" Jump to RN"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Jump from native page to RN When the page is , You need to specify an input parameter pageName, That is to jump RN The name of the page . If you need to pass the reference , Define a dictionary , The key is param, Value is the parameter passed . as follows ,MyPost It's a RN The name of the page ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\/\/  Original jump  RN\nNSDictionary *params = @{@\"pageName\": @\"MyPost\", @\"param\": param};\n[WYMediator routeURL:WYURL(@\"xxx\/rncontainer\") withParams:par];\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\/\/  Original jump \nExtParams param = new ExtParams();\nparam.putStringExtra(\"PageName\", \"MyPost\");\nparam.putStringExtra(\"PageParam\", pageParams);\nRouter.startRoute(activity, \"xxx\/rncontainer\", param);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"RN Jump to the original "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" from RN The page jumps to the native page , And follow our routing protocol . This process is implemented natively , It calls the native routing component , from RN The container jumps to the specified native page . as follows ,name It refers to the protocol corresponding to the native page ,passProps The parameters needed to jump to the native page , If not, don't write . The last parameter is the callback function , When executed push When the method is used , The route will be based on the incoming componentId Save the callback function in JS End , The native page closes and returns to the current RN When the page is , Routing will be based on the current page component Take out the callback function and execute it ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Navigation.push(this.props.componentId, {\n    name:\"xxxx\",\n    passProps:{patientId:'11111', patientName:' Zhang San '}\n}, (componentId, params) => {\n    \/\/  Returns the callback function of the current page \n});\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"RN Jump to RN"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RN Between the jump and RN Jump to the same original , adopt push The method of transmission name、passProps and callback Callback , It's just that the name It's no longer a native agreement , It's the name of the registration below ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Navigation.registerComponent(\"Test\", () => TestScreen);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"RN Page back "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Need to close the current RN The page is simple , Just call pop The method will do ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"Navigation.pop(this.props.componentId, passProps:{})\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"passProps Parameters that need to be passed back to the previous page , Used before push Medium callback Method's callback , If not, don't write ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Container life cycle "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RN The components of Component Has its own life cycle :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"constructor() \/\/ Construction method "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"componentWillMount() \/\/ About to load "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"componentDidMount() \/\/ Loading complete "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"componentWillUnmount() \/\/ Component has been unloaded "}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" But sometimes we just can't meet our business needs , For one RN Pages are important for using a container , The life cycle of the container is also crucial , We need to do something else in the process of hiding the container , So we add the life cycle of container to the new routing framework :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"containerWillAppear() \/\/ The container is about to display "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"containerDidAppear() \/\/ The container is showing "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"containerWillDisappear() \/\/ The container is about to disappear "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"containerDidDisappear() \/\/ The container is hiding "}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" There is no need to prioritize the life cycle of a container and the life cycle of a component , In most cases, it is used when the container needs to be updated when it is displayed again , So it may be called many times ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":" Navigation bar configuration "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" At present, the configuration of the navigation bar is the same as react-native-navigation Frame like , use Tree The navigation parameters are passed into , Set it up . The basic usage is as follows :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"class MyScreen extends Component {\n  static options(passProps) {\n    return {\n      topBar: {\n        title: {\n       text: ' My page '\n      },\n        leftButtons: [\n          {\n            id: 'buttonOne',\n            icon: require('icon.png')\n          }\n        ],\n        rightButtons: [],\n      }\n    };\n  }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RN When the page is loaded , Will be registered according to PageName Get the Component, The above example refers to MyScreen, So as to get the corresponding static variables options, Pass it on to the current RN In the container ,RN The container will generate navigation bar for display according to the configuration inside , In this way, using the native navigation bar can ensure that the page and the native height coincide , And there's no need to add a bridge class just to modify the native navigation bar , Convenient and efficient ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Summary and questions "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" at present WYNavigation It has been put into use in the micro doctor project , No obvious... Was found during the use bug, This solution solves the problem of navigation stack confusion in complex mixed scenes in a simple way . However, due to the current related business involved in the form of a single page , So for others tab、modal And so on, it involves many RN Page level of the situation did not do too much in-depth , But in the future, we hope to make up for this deficiency as soon as possible , Think of it as a universal solution , For more RN And native mixed development projects , I hope you can share with us ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The first figure :Unsplash"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" author : Huang Lili "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" original text :https:\/\/mp.weixin.qq.com\/s\/W75sT2px7P9rHJUrmTwY4g"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" original text :React Native Native hybrid routing solutions "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" source : Front end technology of Micro Medical University - WeChat official account [ID:wed_fed]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Reprint : The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source ."}]}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210531145601315r.html

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless