question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

need help with using elements array in a custom node handler

See original GitHub issue

Hi, I have a custom node with a plus button attached to the node. I want the button to add a new node right below the one with the plus.

I’m creating the handler in the same way as in your example, Custom node example

This issue is that, for me to add a Node, I have to pass the addNode helper an array of the elements state, but because of the way the handler is passed, elements is always equal to [] (because of closures, the function is passed only when it goes through the useEffect, and thus captures it’s surrounding scope at that time).

Do you have any idea about how to use the elements array state in a custom node handler ? Thank you for your help

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MatthieuJnoncommented, Jul 29, 2020

Thank you so much, I’ve been coding with hooks for > 1 year, and I didn’t event know you could pass a function to the set state method… Thank you for your reactivity and for your help

0reactions
moklickcommented, Jul 29, 2020

You can pass a function to setElements like we are doing it in our custom node example. The first parameter is the current elements value. Then you could do:

const handleAddChild = node => {
    setElements(els => {
      // do something with els
      
      return els;
    });
  };

Or you could receive the current elements with the way described above and pass it to the handleAddChildfunction:

import { useStoreState } from 'react-flow-renderer';

const StartNode = props => {
  const elements = useStoreState(state => state.elements); 

  return (
    <>
      <span>Start of your form</span>
      <Icon name="plus circle" onClick={() => props.data.onAddChild(elements, props)} />
    </>
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Tutorial - Node.js Arrays - Java2s.com
To create arrays, you can either use traditional notation or array ... We can specify the index of the element where you want...
Read more >
Array - JavaScript - MDN Web Docs
The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name ...
Read more >
Array of custom object in Node.js - Stack Overflow
One way or the other you need to iterate. The two ways I can think to do it are: The way you did...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
An array is an ordered collection of elements. In JavaScript, an array can be created using formal object notation, or it can be...
Read more >
Array methods - The Modern JavaScript Tutorial
Arrays do not form a separate language type. They are based on objects. So typeof does not help to distinguish a plain object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found