File tree Expand file tree Collapse file tree 4 files changed +54
-1
lines changed Expand file tree Collapse file tree 4 files changed +54
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @headless-tree/react " : patch
3+ ---
4+
5+ Added optional @headless-tree/react /react17 import for useTree for compatibility
Original file line number Diff line number Diff line change @@ -58,6 +58,15 @@ When rendering your tree, don't forget to
5858- Spread the return value of ` item.getProps() ` as props into your tree item elements.
5959- You probably want to indent your items based on their depth, which you can get via ` item.getItemMeta().level ` .
6060
61+ :::info[ React 17]
62+
63+ Headless Tree uses ` useInsertionEffect ` in a way that triggers error logs on React 17. The
64+ best solution is to upgrade to React 18 if possible, otherwise you can also import ` useTree `
65+ from ` @headless-tree/react/react17 ` instead as a workaround. This workaround might be removed
66+ in the future.
67+
68+ :::
69+
6170## Importing Features
6271
6372The architecture of Headless Tree separates individual features into distinct objects, that need to be manually
Original file line number Diff line number Diff line change 1515 "default" : " ./dist/index.d.ts"
1616 }
1717 },
18+ "./react17" : {
19+ "import" : {
20+ "types" : " ./dist/react17/index.d.mts" ,
21+ "default" : " ./dist/react17/index.mjs"
22+ },
23+ "require" : {
24+ "types" : " ./dist/react17/index.js" ,
25+ "default" : " ./dist/react17/index.d.ts"
26+ }
27+ },
1828 "./package.json" : " ./package.json"
1929 },
2030 "sideEffects" : false ,
2131 "scripts" : {
22- "build" : " tsup ./src/index.ts --format esm,cjs --dts" ,
32+ "build" : " tsup ./src/index.ts ./src/react17/index.tsx --format esm,cjs --dts" ,
2333 "start" : " tsup ./src/index.ts --format esm,cjs --dts --watch"
2434 },
2535 "repository" : {
Original file line number Diff line number Diff line change 1+ import { useEffect , useState } from "react" ;
2+ import { TreeConfig , TreeState , createTree } from "@headless-tree/core" ;
3+
4+ export const useTree = < T , > ( config : TreeConfig < T > ) => {
5+ const [ tree ] = useState ( ( ) => ( { current : createTree ( config ) } ) ) ;
6+ const [ state , setState ] = useState < Partial < TreeState < T > > > ( ( ) =>
7+ tree . current . getState ( ) ,
8+ ) ;
9+
10+ useEffect ( ( ) => {
11+ tree . current . rebuildTree ( ) ;
12+ } , [ tree ] ) ; // runs only once after mount
13+
14+ tree . current . setConfig ( ( prev ) => ( {
15+ ...prev ,
16+ ...config ,
17+ state : {
18+ // ...prev.state,
19+ ...state ,
20+ ...config . state ,
21+ } ,
22+ setState : ( state ) => {
23+ setState ( state ) ;
24+ config . setState ?.( state ) ;
25+ } ,
26+ } ) ) ;
27+
28+ return tree . current ;
29+ } ;
You can’t perform that action at this time.
0 commit comments