Skip to content

Commit f92503e

Browse files
rfrerebe-stxalfonsogarciacaro
authored andcommitted
Add documentation to get state back from JS/TS component
1 parent 0514b49 commit f92503e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ Some components have a [Typescript](https://www.typescriptlang.org/) definition
1414
- [1. Install the react component](#1-install-the-react-component)
1515
- [2. Define the props type](#2-define-the-props-type)
1616
- [3. Define the React component creation function](#3-define-the-react-component-creation-function)
17+
- [Member Import](#member-import)
18+
- [Default Import](#default-import)
19+
- [Fields of imported items](#fields-of-imported-items)
20+
- [Directly creating the element](#directly-creating-the-element)
1721
- [4. Use the creation function in your view code](#4-use-the-creation-function-in-your-view-code)
22+
- [5. Get component state back into your code](#5-get-component-state-back-into-your-code)
1823
- [Importing using a Pojo (plain old JS object) record](#importing-using-a-pojo-plain-old-js-object-record)
1924
- [Passing in props as tuples (without a type declaration of the props)](#passing-in-props-as-tuples-without-a-type-declaration-of-the-props)
2025
- [Edgecases](#edgecases)
@@ -154,6 +159,48 @@ let view (model : Model) (dispatch : Msg -> unit) =
154159
[ progressLine [ Percent model.currentProgress; StrokeColor "red" ] [] ]
155160
```
156161

162+
### 5. Get component state back into your code
163+
164+
If you want to get from your component state back in F# code, you need to follow react documentation :
165+
https://reactjs.org/docs/lifting-state-up.html to propagate component state to upper components.
166+
167+
Insert a function in your props to get state back.
168+
```fsharp
169+
// Define function in props
170+
type ComponentProps =
171+
| GetState of (DateTime -> unit)
172+
| ...
173+
174+
// sample function matching props signature
175+
let logDateTimeSelected (d : DateTime) =
176+
Fable.Import.Browser.console.log (sprintf "Date : %A" d)
177+
178+
// Component definition (here a calendar)
179+
let inline Calendar (props : ComponentProps list) : Fable.Import.React.ReactElement =
180+
ofImport "default" "./CalendarComponent.tsx" (keyValueList CaseRules.LowerFirst props) []
181+
182+
// Component initialization with props
183+
div [] [
184+
Calendar [ GetState logDateTimeSelected ]
185+
]
186+
```
187+
188+
On Javascript/TypeScript side, you need to use this props within your component
189+
```js
190+
// Define component props
191+
export interface IComponentProps {
192+
// new props matching F# ComponentProps definition
193+
getState : ((date : Date) => void)
194+
...
195+
}
196+
197+
// within JS/TS event handler use your props function
198+
private _onSelectDate(date: Date): void {
199+
this.props.getState(date)
200+
...
201+
}
202+
```
203+
157204
## Importing using a Pojo (plain old JS object) record
158205

159206
The Pojo import is similar to the approach above, but instead of declaring a DU you create a [Pojo record](http://fable.io/docs/interacting.html#plain-old-javascript-objects). Using a record with the Pojo attribute to express the props looks more like idiomatic F# code but it can be unwieldy if you have a lot of optional props. Since this is common with React components, using the DU approach above can often be more convenient.

0 commit comments

Comments
 (0)