You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/recipes/javascript/third-party-react-package.md
+15-4Lines changed: 15 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
To use a third-party React library in a SAFE application, you need to write an F# wrapper around it. There are two ways for doing this - using [Feliz](https://zaid-ajaj.github.io/Feliz/) or using [Fable.React](https://www.nuget.org/packages/Fable.React/).
2
+
2
3
## Prerequisites
3
4
4
5
This recipe uses the [react-d3-speedometer NPM package](https://www.npmjs.com/package/react-d3-speedometer) for demonstration purposes. [Add it to your Client](../package-management/add-npm-package-to-client.md) before continuing.
-`createElement` from `Feliz.ReactApi.IReactApi` takes the component you're wrapping react-d3-speedometer, the props that component takes and creates a ReactComponent we can use in F#.
25
27
-`importDefault` from ` Fable.Core.JsInterop` is giving us access to the component and is equivalent to
28
+
26
29
```javascript
27
30
importReactSpeedometerfrom"react-d3-speedometer"
28
31
```
32
+
29
33
The reason for using `importDefault` is the documentation for the component uses a default export "ReactSpeedometer". Please find a list of common import statetments at the end of this recipe
30
34
31
35
As a quick check to ensure that the library is being imported and we have no typos you can `console.log` the following at the top within the view function
In the console window (which can be reached by right-clicking and selecting Insepct Element) you should see some output from the above log.
36
42
If nothing is being seen you may need a slightly different import statement, [please refer to this recipe](../../v4-recipes/javascript/import-js-module.md).
37
43
@@ -44,7 +50,9 @@ createObj [
44
50
"maxValue" ==> 10
45
51
]
46
52
```
53
+
47
54
Is equivalent to
55
+
48
56
```javascript
49
57
{ minValue:0, maxValue:10 }
50
58
```
@@ -58,7 +66,7 @@ For a full detailed tutorial head over to [this blog post](https://www.compositi
58
66
59
67
## Fable.React - Setup
60
68
61
-
####1. Create a new file
69
+
### 1. Create a new file
62
70
63
71
Create an empty file named `ReactSpeedometer.fs` in the Client project above `Index.fs` and insert the following statements at the beginning of the file.
64
72
@@ -70,7 +78,8 @@ open Fable.Core.JsInterop
70
78
open Fable.React
71
79
```
72
80
73
-
#### 2. Define the Props
81
+
### 2. Define the Props
82
+
74
83
Prop represents the props of the React component. In this recipe, we're using [the props listed here](https://www.npmjs.com/package/react-d3-speedometer) for `react-d3-speedometer`. We model them in Fable.React using a discriminated union.
75
84
76
85
```fsharp
@@ -85,7 +94,8 @@ type Prop =
85
94
>
86
95
> Note that we can model any props here, both simple values and "event handler"-style ones.
87
96
88
-
#### 3. Write the Component
97
+
### 3. Write the Component
98
+
89
99
Add the following function to the file. Note that the last argument passed into the `ofImport` function is a list of `ReactElements` to be used as children of the react component. In this case, we are passing an empty list since the component doesn't have children.
0 commit comments