"SetPage" duplicated when dispatched through Cmd.ofMsg
See original GitHub issueWhen the router is connected to the Elmish state using Router.infer
, routing commands issued through Cmd.ofMsg are duplicated.
Here is a minimal example (see github):
type Page =
| [<EndPoint "/">] Home
| [<EndPoint "/secondPage">] NewPage
type Message =
| SetPage of Page
| DoAThing
let update message model =
// This demonstrates which messages are duplicated
Console.WriteLine($"{message}")
match message with
| SetPage page ->
{model with page = page}, Cmd.none
| DoAThing ->
model, Cmd.ofMsg (SetPage NewPage)
let router = Router.infer SetPage (fun model -> model.page)
When DoAThing
is dispatched from Home
, SetPage NewPage
gets dispatched twice. This causes unexpected behavior whenever one wants to trigger a redirect after performing an action, i.e. create a new resource and then navigate to the corresponding view.
Issue Analytics
- State:
- Created 4 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
"SetPage" duplicated when dispatched through Cmd.ofMsg
When the router is connected to the Elmish state using Router.infer , routing commands issued through Cmd.ofMsg are duplicated.
Read more >My tips for working with Elmish
Use Cmd. performFunc to evaluate a simple function and map the success to a message discarding any possible error.
Read more >Page
Page provides methods to interact with a single tab in a Browser, or an extension background page in Chromium. One Browser instance might...
Read more >Cmd.ofMsg is gone. Why? · Issue #170 · elmish ...
The message to message to send when the task finish with success. While Cmd.OfFunc.result just takes a message and dispatch it back. I...
Read more >ACUCOBOL-GT
ACUCOBOL-GT supports the emulation of graphical controls and windows on character-based systems. This emulation allows you to more easily write.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, we can’t really detect that a URL change was caused by the same message that it triggers. So it’s best to avoid dispatching it directly. Instead you can either have a separate message like you did, or directly set
{ model with page = page }
in the update forDoAThing
.Maybe the documentation should recommend calling this message something like
PageChanged
, to emphasize that it’s not meant to be dispatched directly.I have not used routing myself, but as far as I understand it, the SetPage message is indeed not meant to be used by you, but only by the router. It is only a way to flexibly configure the router to manipulate the model in the right way.