![]() |
VOOZH | about |
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" />
<PackageVersion Include="Elmish.Store" Version="0.1.0" />Directory.Packages.props
<PackageReference Include="Elmish.Store" />Project file
paket add Elmish.Store --version 0.1.0
#r "nuget: Elmish.Store, 0.1.0"
#:package Elmish.Store@0.1.0
#addin nuget:?package=Elmish.Store&version=0.1.0Install as a Cake Addin
#tool nuget:?package=Elmish.Store&version=0.1.0Install as a Cake Tool
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:
Elmish Store addresses these issues by enabling more targeted data management and rendering, leading to cleaner code and improved performance.
The package consists of two parts:
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 leverages React.memo to store the previous selector result and compares it with the new one depending on the chosen equality mode:
equality constraintisEqual functionopen 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
[<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 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 5,141 | 1/19/2024 |