Skip to content

Commit 32e71e9

Browse files
committed
feat: add workaround for react 17
1 parent 662e2a8 commit 32e71e9

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

.changeset/rich-bees-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@headless-tree/react": patch
3+
---
4+
5+
Added optional @headless-tree/react/react17 import for useTree for compatibility

packages/docs/docs/0-root/getstarted.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

6372
The architecture of Headless Tree separates individual features into distinct objects, that need to be manually

packages/react/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
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": {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)