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

Changes to the dndRootElement prop do not update the Tree #263

Open
goliney opened this issue Jul 30, 2024 · 1 comment
Open

Changes to the dndRootElement prop do not update the Tree #263

goliney opened this issue Jul 30, 2024 · 1 comment

Comments

@goliney
Copy link

goliney commented Jul 30, 2024

If you mount <Tree dndRootElement={dndRootElement.current} /> when dndRootElement.current is null, changes will not be picked up when dndRootElement.current start to refer to a real element.

@ru13r
Copy link

ru13r commented Dec 22, 2024

Maybe someone finds this useful. I don't know whether I did it optimally, but I wrote a custom hook to timeout rendering of children before the container is mounted.

import { useEffect, useState } from 'react';

const useIsParentMounted = (): boolean => {
  const [isParentMounted, setIsParentMounted] = useState(false);

  useEffect(() => {
    // Delay rendering of the child
    const timeout = setTimeout(() => {
      setIsParentMounted(true);
    }, 0); // Ensures the parent is mounted first
    return () => clearTimeout(timeout);
  }, []);

  return isParentMounted;
};

export default useIsParentMounted;

Now the <Tree /> can be rendered only after the parent container is mounted and has a non-null reference:

const containerRef = useRef<any>(null);
const isParentMounted = useIsParentMounted();
return (
  <div ref={containerRef}>
    {isParentMounted && <Tree 
      dndRootElement={containerRef.current} 
      // ... 
      />}
  </div>
  );

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