{"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 ."}]}]}