Skip to content

Commit 4184f31

Browse files
committed
Always separate md elements with whitespace; do not skip header levels
1 parent 6d7a0d3 commit 4184f31

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

docs/recipes/javascript/third-party-react-package.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
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+
23
## Prerequisites
34

45
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.
@@ -13,6 +14,7 @@ open Fable.Core.JsInterop
1314
```
1415

1516
Within the view function
17+
1618
```fsharp
1719
Feliz.Interop.reactApi.createElement (importDefault "react-d3-speedometer", createObj [
1820
"minValue" ==> 0
@@ -23,15 +25,19 @@ Feliz.Interop.reactApi.createElement (importDefault "react-d3-speedometer", crea
2325

2426
- `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#.
2527
- `importDefault` from ` Fable.Core.JsInterop` is giving us access to the component and is equivalent to
28+
2629
```javascript
2730
import ReactSpeedometer from "react-d3-speedometer"
2831
```
32+
2933
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
3034

3135
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
36+
3237
```fsharp
3338
Browser.Dom.console.log("REACT-D3-IMPORT", importDefault "react-d3-speedometer")
3439
```
40+
3541
In the console window (which can be reached by right-clicking and selecting Insepct Element) you should see some output from the above log.
3642
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).
3743

@@ -44,7 +50,9 @@ createObj [
4450
"maxValue" ==> 10
4551
]
4652
```
53+
4754
Is equivalent to
55+
4856
```javascript
4957
{ minValue: 0, maxValue: 10 }
5058
```
@@ -58,7 +66,7 @@ For a full detailed tutorial head over to [this blog post](https://www.compositi
5866

5967
## Fable.React - Setup
6068

61-
#### 1. Create a new file
69+
### 1. Create a new file
6270

6371
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.
6472

@@ -70,7 +78,8 @@ open Fable.Core.JsInterop
7078
open Fable.React
7179
```
7280

73-
#### 2. Define the Props
81+
### 2. Define the Props
82+
7483
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.
7584

7685
```fsharp
@@ -85,7 +94,8 @@ type Prop =
8594
>
8695
> Note that we can model any props here, both simple values and "event handler"-style ones.
8796
88-
#### 3. Write the Component
97+
### 3. Write the Component
98+
8999
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.
90100

91101
```fsharp
@@ -94,7 +104,8 @@ let reactSpeedometer (props : Prop list) : ReactElement =
94104
ofImport "default" "react-d3-speedometer" propsObject [] // import the default function/object from react-d3-speedometer
95105
```
96106

97-
#### 4. Use the Component
107+
### 4. Use the Component
108+
98109
With all these in place, you can use the React element in your client like so:
99110

100111
```fsharp

0 commit comments

Comments
 (0)