Skip to content

Commit f88fd42

Browse files
0x53Aalfonsogarciacaro
authored andcommitted
Put more emphasis on there being multiple exports/imports, add example for default import (#92)
* Add example for default import * Update using-third-party-react-components.md * Update using-third-party-react-components.md
1 parent 9339529 commit f88fd42

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

docs/using-third-party-react-components.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ type SomeComponentProps =
6464

6565
### 3. Define the React component creation function
6666

67+
There are several different ways to declare exports in Javascript (default imports, member imports, namespace imports); depending on how the Javascript React component was declared, you have to choose the right import. Refer to the [Fable docs](http://fable.io/docs/interacting.html#importing-javascript-code) for more information on imports.
68+
6769
Using the `ofImport` function you instruct Fable which component should be instantiated when the creation function is called.
6870

69-
There are several different ways to declare exports in Javascript (default imports, member imports, namespace imports); depending on how the Javascript React component was declared, you have to choose the right import. Refer to the [Fable docs](http://fable.io/docs/interacting.html#importing-javascript-code) for more information on imports.
71+
#### Member Import
7072

7173
In the example of rc-progress, to declare a `progressLine` creation function that imports the `Line` component from the library `rc-progress`, you would declare it as follows.
7274

@@ -84,6 +86,33 @@ The `keyValueList` function is used to convert the props of type `IProgressProps
8486

8587
In the docs of the [rc-progress](https://github.com/react-component/progress) React component the import style used is a *member import* (e.g. `import { Line, Circle } from 'rc-progress';`), so we refer to the component member `Line` directly in the ofImport expression.
8688

89+
#### Default Import
90+
91+
If the export is declard as a default export, then you would use ``"default"`` as the member name.
92+
Taking [react-native-qrcode-scanner](https://github.com/moaazsidat/react-native-qrcode-scanner) as an example:
93+
94+
To translate the example
95+
```js
96+
import QRCodeScanner from 'react-native-qrcode-scanner';
97+
```
98+
you would declare your function like
99+
100+
```fsharp
101+
let inline qr_code_scanner (props : QRCodeScannerProps list) : ReactElement =
102+
ofImport "default" "react-native-qrcode-scanner" (keyValueList CaseRules.LowerFirst props) []
103+
```
104+
105+
#### Directly creating the element
106+
107+
If you already have a reference to the imported component, then you can also use ``createElement``.
108+
109+
The default import above could also be rewritten like this:
110+
111+
```fsharp
112+
let rnqs = importDefault "react-native-qrcode-scanner"
113+
createElement(rnqs, (keyValueList CaseRules.LowerFirst props), [])
114+
```
115+
87116
### 4. Use the creation function in your view code
88117

89118
The function you declared in step 2 now behaves just like any other React component function.

0 commit comments

Comments
 (0)