naisutech / react-tree

Hierarchical tree component for React in Typescript
https://naisutech.github.io/react-tree/
MIT License
206 stars 25 forks source link

Expanding node by default #53

Closed psorensen closed 2 years ago

psorensen commented 2 years ago

Hello, and thanks for the awesome component!

Is it possible to open a specific node on initialization? This would be particularly helpful for direct linking to folder states. Thanks!

yokeshwar93 commented 2 years ago

I'm looking for this feature as well

psorensen commented 2 years ago

@yokeshwar93 I found this can be accomplished using useEffect(). Be sure to pass an empty array as the second parameter to prevent infinite updates.


<Tree nodes={[...]}>
  {(controls) => {
    useEffect(() => {
      if ( postParent ) { // grabbing post parent from URL query params
        controls.toggleNodeSelection(parseInt(postParent))
        controls.toggleOpenCloseNode(parseInt(postParent))
      }
    }, []);
  }}
</Tree>
TianhongZhang commented 2 years ago

@psorensen , I tried your work around and get this error: React Hook "useEffect" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function. I am using react and react-dom version 16.13.0. Do I need to upgrade react?