Elmish.Store
0.1.0
dotnet add package Elmish.Store --version 0.1.0
NuGet\Install-Package Elmish.Store -Version 0.1.0
<PackageReference Include="Elmish.Store" Version="0.1.0" />
paket add Elmish.Store --version 0.1.0
#r "nuget: Elmish.Store, 0.1.0"
// Install Elmish.Store as a Cake Addin #addin nuget:?package=Elmish.Store&version=0.1.0 // Install Elmish.Store as a Cake Tool #tool nuget:?package=Elmish.Store&version=0.1.0
Elmish Store
A library that merges Elmish with React, offering an external store for efficient and selective component rendering.
This project is designed to tackle two primary challenges commonly faced when integrating Elmish and React:
- Props Drilling: Avoiding the complexities of passing data through multiple layers of components.
- Excessive Rendering: Minimizing unnecessary component renders often caused by passing the entire model from the top down.
Elmish Store addresses these issues by enabling more targeted data management and rendering, leading to cleaner code and improved performance.
Usage
The package consists of two parts:
- Creating the store: Functions that accept the user's program definition and create a store consumable by custom React hooks.
- Selectors: Custom hooks that allow selecting parts of the model with the assurance that components will re-render only if the selected part changes. These come in two variants:
useSelector
: This expects a stable selector function and uses reference equality to compare the results with the previous one.useSelectorMemoized
: This expects any kind of function. It leveragesReact.memo
to store the previous selector result and compares it with the new one depending on the chosen equality mode:- Generic
equality
constraint - Custom
isEqual
function
- Generic
Example
Store Definition + Selector Helpers:
open Elmish
open ElmishStore
open ElmishStore.Example.Model
open Feliz
#if DEBUG
open Elmish.Debug
#endif
let store =
Program.mkProgram init update (fun _ _ -> ())
#if DEBUG
|> Program.withConsoleTrace
|> Program.withDebugger
#endif
|> ElmishStore.createStore "main"
[<Hook>]
let useSelector (selector: Model -> 'a) = React.useElmishStore (store, selector)
[<Hook>]
let useSelectorMemoized (memoizedSelector: Model -> 'a) =
React.useElmishStoreMemoized (store, memoizedSelector)
let dispatch = store.Dispatch
Usage in components:
[<ReactComponent>]
let private Counter1 () =
let counter = ModelStore.useSelector (_.Counter1) // only rerenders if Counter1 changes
Html.div [
prop.className "border flex flex-col items-center justify-center gap-4 p-4"
prop.children [
Html.span [
prop.className "text-xl"
prop.text $"Counter 1: %i{counter}"
]
Html.button [
prop.className "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
prop.onClick (fun _ -> Increment1 |> ModelStore.dispatch)
prop.text "Increment"
]
]
]
[<ReactComponent>]
let private CounterSum () =
// Function generating a new tuple each time. It uses F# built-in equality compare function.
let counter1, counter2 = ModelStore.useSelectorMemoized (fun m -> (m.Counter1, m.Counter2))
Html.div [
prop.className "border p-4 text-xl"
prop.text $"Counters Sum: %i{counter1 + counter2}"
]
This GIF illustrates the example (notice how React DevTools can be utilized to inspect and debug state changes):
For a comprehensive understanding, explore the ElmishStore.Example
directory within this repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Fable.Elmish (>= 4.1.0)
- Feliz (>= 2.7.0)
- Feliz.CompilerPlugins (>= 2.2.0)
- FSharp.Core (>= 8.0.100)
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.1.0 | 865 | 1/19/2024 |