Confusing behaviour of emit and .withhandler
See original GitHub issueHello,
Thanks for creating react4s. Really liked it.
- I find behaviour of emit and .withhandler confusing. Documentation says
Instead of updating state locally, you can choose to emit() a message to the parent component, telling it how to update the model for the inner component
on this page. Also if I see emit documentation in IntelliJ I find -
/** Emit a message of type M, which can be handled by a parent component by using .withHandler(...). */ var emit : M => Unit = { _ => }
This understanding and code implemented in Tree editor example is not aligned. TreeNodeComponent emits event in renderButton method and is handled in same component (NOT parent component) in renderTodoList. Can you please explain this behaviour?
- Library documentation says
React4s has an Elm/Redux-like emit() system
. Does that mean it provide redux like store (single place to store state) and any component (not only parent component) can listen to state change??
Issue Analytics
- State:
- Created 5 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Reading 6.2 – Are you confused? - Talking About Behavior
One involves responding to private physiological events corresponding to “confusing” situations by labeling these events as a feeling of confusion, as taught by ......
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
Well, consider the case of a simple toggle button.
We can write it using a global store for the application:
The problem is, now the toggle button mentions
autosave
andSetAutosave
. So it can only be used to toggle whether or not the application autosaves. If I wanted another toggle button to toggle whether or not to check spelling, eg.global.spellCheck
, I’d have to create a modified copy of theToggleButtonComponent
code!If instead it’s written with props and local emit, as in the example here, I could simply use the same ToggleButtonComponent for both switches:
Here’s a quick implementation. I’m not an expert on Redux, so you’ll have to tell me if it lacks anything.
Implementation
This is a general class for all global stores, with messages of type M and state of type S:
In short, it keeps track of who’s listening for state updates, and expects
onEmit
to be overriden to define what happens when a message is emitted.Usage
Define a single concrete store for your application somewhere. Here you’ll specify how to update the state whenever a message arrives.
Of course, in realistic examples, the Boolean would be sum type representing the possible messages as in the tree editor example, and the state would likely be a record instead of an Int.
Once you have done this, you can access the state and emit from all your components like this: