Merge values from collector function into Node
See original GitHub issueIs your feature request related to a problem? Please describe.
I want to use a value returned from useNode’s hook collector function in a canDrag
rule, but since it’s available only in the hook’s returned object, I am forced to declare and run the calculations at multiple places.
Describe the solution you’d like
The values returned from useNode’s hook collector function are useful even outside of the components scope. It would be handy if they were merged into the Node itself.
const Hero = ({ title, ...rest }) => {
const { connectors: { connect, drag }, setProp, tainted } = useNode(node => ({
tainted: node.data.props.newTitle && title !== node.data.props.newTitle }
}));
return (
<div>
<h2>Edit me!</h2>
<p contentEditable={true} onKeyUp={(e) => {
setProp(props => {
props.newTitle = e.target.innerText;
})
}}>{title}</p>
<p style={{color: tainted ? 'black' : 'green'}}>{rest.newTitle}</p>
<button ref={ref => connect(drag(ref))}>{tainted ? 'Drag me?' : "Don't touchme!"}</button>
</div>
)
}
Hero.craft = {
defaultProps: {
title: 'Sphinx of black quartz judge my vow!'
},
rules: {
canDrag: node => node.data.someFittingPropertyName.tainted
}
}
Additional context
At first I thought that node.data.custom
might contain it, but it’s always empty.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Node.js — How to Merge Objects - Future Studio
This tutorial shows you how to merge objects in JavaScript. ... Arrays; Promises; JSON; Iterators; Classes; Numbers; Objects; File System ...
Read more >How to write the merge function for Collectors.toMap()
The 3rd argument to Collectors.toMap() is the merge function, which is a function that gets two values of the Map , and returns...
Read more >Collectors toMap() method in Java with Examples
The task of the function is to merge the values having the same key in a way defined by the coder. This overloaded...
Read more >Java - Combine Multiple Collections - Baeldung
A quick and practical guide to combining multiple collections in Java.
Read more >composable streams/iterators · Issue #37729 · nodejs/node · GitHub
First, we have microtask-collector , which is similar to your WritableStreamToIterator class, but it allows external producers to add values to the current ......
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 Free
Top 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
This is similar to the
Container
andCard
component in the Basic Tutorial.Let’s say our
TextWrapper
component were to specify a slightly different collector function:Now, based on your proposal, if we were to save the collected values to the Node itself, the collected values in
TextComponent
will override the ones made byHero
Merging the collected values into the Node would not be ideal, since child components would share the same Node context, thus they would also be able to use the
useNode
hook and their collected values will be merged into the Node as well.Instead, you could consider using
custom
values which is something like state values but for the Node itself. (I just realized I forgot to document how to use this, so thank you again! 🤣 🙈 )Basically, like this: