Fable.Elmish.ElmishToReact 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Fable.Elmish.ElmishToReact --version 0.0.1
NuGet\Install-Package Fable.Elmish.ElmishToReact -Version 0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fable.Elmish.ElmishToReact" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fable.Elmish.ElmishToReact --version 0.0.1
#r "nuget: Fable.Elmish.ElmishToReact, 0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Fable.Elmish.ElmishToReact as a Cake Addin
#addin nuget:?package=Fable.Elmish.ElmishToReact&version=0.0.1

// Install Fable.Elmish.ElmishToReact as a Cake Tool
#tool nuget:?package=Fable.Elmish.ElmishToReact&version=0.0.1

elmish-to-react

Wrap an Elmish program as a React component.

This is helpful when writing a React app that needs to render an Elmish program as a reuseable component.

A basic example

Step 1: Write a Elmish program in F#

Follow the Elmish basic instructions to build your program in F#.

let myProgram = Program.mkProgram init update view

Step 2: Wrap your program into a React component

open Fable.Elmish.ElmishToReact
let MyProgramComponent = elmishToReactSimple myProgram

Step 3: Start using your component

From your javascript/react code you can render your wrapped program just like any regular React component:

const WrapperComponent = (props) =>
  <div>
    <p>Here's the wrapped program:</p>
    <MyProgramComponent/>
  </div>

ReactDOM.render(<WrapperComponent/>, element);

Passing Props

In React we like to pass props into child components. The child should re-render when there is a change in the props it receives from the parent.

How can we pass in props to our Elmish program and trigger a re-render?

Let's rewrite our program so it is ready to receive props. The props includes a label string and a saySomething function is used to pass information from the child back up the parent.

type Props =
  abstract label : string
  abstract saySomething : string -> unit

type Model =
  { Props : Props
    // ...plus any other model attributes you need
  }

type Msg =
  | UpdateProps of Props
  // ...plus any other Msg types you need

let init props =
  { Props = props }, Cmd.none

let update msg model =
  match msg with
  | UpdateProps props ->
      { model with Props = props }, Cmd.none

let program = Program.mkProgram init update view

Next we "externalise" our program. This allows us to externally feed a props message into the Elmish program:

open Fable.Elmish.ElmishToReact

let externalisedProgram =
  Externalised.externalise program
  |> Externalised.withPropsMsg UpdateProps

let MyProgramComponent = elmishToReact externalisedProgram

In our javascript code we can pass the props into our wrapped program:

ReactDom.render(
    <MyProgramComponent label="Hello, world"
                        saySomething={(msg) => console.log(msg)}/>,
  element)

Unmounting

React might need to repeatedly mount and unmount the Elmish program as the user navigates around a single page app. The Elmish program might want to clean-up its resources each time it is unmounted.

Let's rewrite our program so it can receive an unmount message:

type Msg =
  | Unmount
  // ...plus any other Msg types you need

let update msg model =
  match msg with
  | Unmount ->
      // Do any clean-up here
      model, Cmd.none

let program = Program.mkProgram init update view

Next we add the Unmount message to our externalised program.

open Fable.Elmish.ElmishToReact

let externalisedProgram =
  Externalised.externalise program
  |> Externalised.withUnmountMsg Unmount

let MyProgramComponent = ElmishComponent.elmishToReact externalisedProgram

A complete example

Example.fs contains a example of an Elmish program demonstrating all the features described above.

example.js is an example of calling the elmish program with react syntax.

To run the example....

yarn run start

...and open http://localhost:8080 in your web browser

Installation with npm

npm install --save-dev elmish-to-react
dotnet add reference ./node_modules/elmish-to-react/Fable.Elmish.ElmishToReact.fsproj
dotnet restore

Installation with paket

paket add nuget Fable.Elmish.ElmishToReact -i
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.2 1,345 12/16/2019
0.0.1 480 9/2/2019