|
1 | | -# Brownfield Navigation JavaScript Usage |
| 1 | +# JavaScript Usage |
2 | 2 |
|
3 | | -## Discoverability triggers |
| 3 | +Use this file for React Native call sites and JS-facing failures. |
4 | 4 |
|
5 | | -- "how to call BrownfieldNavigation from JS" |
6 | | -- "`undefined is not a function` on a Brownfield method" |
7 | | -- "JS method missing after updating `brownfield.navigation.ts`" |
8 | | -- "Brownfield JS method exists but opens wrong destination" |
9 | | - |
10 | | -## Scope |
11 | | - |
12 | | -In scope: |
13 | | -- Calling `BrownfieldNavigation.<method>()` from React Native code. |
14 | | -- JS call-site patterns for buttons/screens and parameter passing. |
15 | | -- Runtime troubleshooting for JS-facing failures (`undefined is not a function`, missing methods, API drift signals). |
16 | | -- Reminding users when contract changes require codegen and native rebuild. |
17 | | - |
18 | | -Out of scope: |
19 | | -- Authoring `brownfield.navigation.ts` and codegen mechanics. For that, read [`setup-codegen.md`](setup-codegen.md) in this folder. |
20 | | -- Android/iOS delegate implementation and startup registration details. For that, read [`native-integration.md`](native-integration.md) in this folder. |
21 | | - |
22 | | -## Procedure |
23 | | - |
24 | | -1. Confirm readiness before discussing JS calls |
25 | | - - Native delegate registration must already be in place before JS uses the module. |
26 | | - - If registration/startup order is uncertain, read [`native-integration.md`](native-integration.md) in this folder. |
27 | | - |
28 | | -2. Provide the default JS invocation pattern |
29 | | - - Import `BrownfieldNavigation` from `@callstack/brownfield-navigation`. |
30 | | - - Call generated methods directly from handlers (for example, button `onPress`). |
31 | | - - Keep method names and argument shape aligned with the generated API. |
32 | | - |
33 | | -3. Recommend call-site best practices |
34 | | - - Pass stable explicit params (`userId`, IDs, flags), not transient UI-derived data. |
35 | | - - Keep each JS method call mapped to a clearly named native destination. |
36 | | - - Use simple direct calls first; avoid wrapping in unnecessary abstractions while debugging. |
37 | | - |
38 | | -4. Apply troubleshooting flow for runtime failures |
39 | | - - `undefined is not a function`: method changed in spec but codegen/rebuild not reapplied. |
40 | | - - Native crash on call: likely delegate registration/startup ordering issue; hand off implementation details to [`native-integration.md`](native-integration.md) in this folder. |
41 | | - - Method exists but wrong route/no-op: generated method present, but native delegate wiring likely incorrect; route delegate fixes to [`native-integration.md`](native-integration.md) in this folder. |
42 | | - |
43 | | -5. Enforce regeneration rule when JS/native API drift appears |
44 | | - - If method names, params, or return types changed in `brownfield.navigation.ts`, rerun: |
45 | | - `npx brownfield navigation:codegen` |
46 | | - - Then rebuild native apps before retesting JS calls. |
47 | | - |
48 | | -6. Route non-JS root causes quickly |
49 | | - - Spec placement/signature/codegen output questions → [`setup-codegen.md`](setup-codegen.md) in this folder. |
50 | | - - Delegate implementation/registration/lifecycle questions → [`native-integration.md`](native-integration.md) in this folder. |
51 | | - |
52 | | -## Minimal TSX example |
53 | | - |
54 | | -Assume the generated contract includes a method like `openNativeProfile(userId: string): void`. The exact method names and params come from the generated `@callstack/brownfield-navigation` module after running codegen. |
| 5 | +## Call pattern |
55 | 6 |
|
56 | 7 | ```tsx |
57 | | -import React from 'react'; |
58 | | -import {Button, SafeAreaView} from 'react-native'; |
59 | 8 | import BrownfieldNavigation from '@callstack/brownfield-navigation'; |
60 | 9 |
|
61 | | -export function ProfileLauncherScreen(): React.JSX.Element { |
62 | | - return ( |
63 | | - <SafeAreaView> |
64 | | - <Button |
65 | | - title="Open native profile" |
66 | | - onPress={() => { |
67 | | - BrownfieldNavigation.openNativeProfile('user-123'); |
68 | | - }} |
69 | | - /> |
70 | | - </SafeAreaView> |
71 | | - ); |
72 | | -} |
| 10 | +BrownfieldNavigation.openNativeProfile('user-123'); |
73 | 11 | ``` |
74 | 12 |
|
75 | | -Portable takeaways: |
76 | | -- Import the generated module from `@callstack/brownfield-navigation`. |
77 | | -- Call the generated method directly from a user action such as `onPress`. |
78 | | -- If this method is missing or throws `undefined is not a function`, treat it as a codegen/rebuild drift signal first. |
| 13 | +## Checklist |
| 14 | + |
| 15 | +- Call generated methods directly from user actions first |
| 16 | +- Keep params explicit and stable |
| 17 | +- Keep method names aligned with generated API |
| 18 | + |
| 19 | +## Fast triage |
| 20 | + |
| 21 | +- `undefined is not a function`: |
| 22 | + - Usually codegen/rebuild drift after contract edits |
| 23 | +- Method exists but opens wrong screen: |
| 24 | + - Usually delegate wiring issue (see native docs) |
| 25 | +- Crash on first call: |
| 26 | + - Often delegate registration order issue (see native docs) |
79 | 27 |
|
80 | | -## Quick reference |
| 28 | +## Recovery order |
81 | 29 |
|
82 | | -- Import: `import BrownfieldNavigation from '@callstack/brownfield-navigation'` |
83 | | -- Typical calls: |
84 | | - - `BrownfieldNavigation.navigateToSettings()` |
85 | | - - `BrownfieldNavigation.navigateToReferrals('user-123')` |
86 | | -- Regenerate on contract change: `npx brownfield navigation:codegen` |
87 | | -- Retest order: |
88 | | - 1. Confirm contract shape |
89 | | - 2. Regenerate |
90 | | - 3. Rebuild native apps |
91 | | - 4. Retest JS call sites |
92 | | -- Error cues this skill should handle first: |
93 | | - - `undefined is not a function` on a Brownfield method |
94 | | - - JS method missing after updating `brownfield.navigation.ts` |
95 | | - - JS call parameters appear out of sync with generated API |
| 30 | +1. Confirm contract shape |
| 31 | +2. Run `npx brownfield navigation:codegen` |
| 32 | +3. Rebuild iOS/Android |
| 33 | +4. Retest JS call |
0 commit comments