VOOZH about

URL: https://www.nuget.org/packages/Elmish.Store/

⇱ NuGet Gallery | Elmish.Store 0.1.0




Elmish.Store 0.1.0

dotnet add package Elmish.Store --version 0.1.0
 
 
NuGet\Install-Package Elmish.Store -Version 0.1.0
 
 
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="Elmish.Store" Version="0.1.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Elmish.Store" Version="0.1.0" />
 
Directory.Packages.props
<PackageReference Include="Elmish.Store" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Elmish.Store --version 0.1.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Elmish.Store, 0.1.0"
 
 
#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.
#:package Elmish.Store@0.1.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Elmish.Store&version=0.1.0
 
Install as a Cake Addin
#tool nuget:?package=Elmish.Store&version=0.1.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 leverages React.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

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 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. 
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.1.0 5,141 1/19/2024