Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add new items? #29

Open
Nasta4ka opened this issue Nov 16, 2023 · 1 comment
Open

Is it possible to add new items? #29

Nasta4ka opened this issue Nov 16, 2023 · 1 comment

Comments

@Nasta4ka
Copy link

thank you for the sortable tree! 👍🏻

What is a safe way to add one more child to an item?

trying to change items i get a warning "The final argument passed to useLayoutEffect changed size between renders." and nothing renders.

@Shaddix
Copy link
Owner

Shaddix commented Nov 19, 2023

Does the following work for you without warnings?

export const TreeRootWithChangeableItems: React.FC<Props> = (props) => {
  const [items, setItems] = useState(initialItems);

  return (
    <>
      <button
        onClick={() => {
          setItems((oldItems) => {
            return [
              {
                id: 'NewItem' + new Date().getTime(),
                text: 'New Item',
                date: new Date(),
                children: [],
              },
              ...oldItems,
            ];
          });
        }}
      >
        Add Item
      </button>
      <SortableTree
        items={items}
        onItemsChanged={setItems}
        TreeItemComponent={TreeItem}
        {...props}
      />
    </>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants