Skip to content

Commit fa12f13

Browse files
committed
fix: dont update when component is unmounted (#278)
1 parent a2f37b1 commit fa12f13

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

next-release-notes.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
<!--
2-
### Breaking Changes
3-
41
### Features
52

63
### Bug Fixes and Improvements
7-
8-
### Other Changes
9-
-->
4+
- Fixes a bug where a state property is updated when the component is unmounted (#278)

packages/core/src/uncontrolledEnvironment/UncontrolledTreeEnvironment.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../types';
1010
import { ControlledTreeEnvironment } from '../controlledEnvironment/ControlledTreeEnvironment';
1111
import { CompleteTreeDataProvider } from './CompleteTreeDataProvider';
12+
import { useIsMounted } from '../useIsMounted';
1213

1314
/* const createCompleteDataProvider = (provider: TreeDataProvider): CompleteTreeDataProvider => ({ // TODO Write class that internally uses provider instead
1415
...provider,
@@ -32,12 +33,14 @@ export const UncontrolledTreeEnvironment = React.forwardRef<
3233
() => new CompleteTreeDataProvider(props.dataProvider),
3334
[props.dataProvider]
3435
);
36+
const isMounted = useIsMounted();
3537

36-
const writeItems = useMemo(
37-
() => (newItems: Record<TreeItemIndex, TreeItem>) => {
38+
const writeItems = useCallback(
39+
(newItems: Record<TreeItemIndex, TreeItem>) => {
40+
if (!isMounted.current) return;
3841
setCurrentItems(oldItems => ({ ...oldItems, ...newItems }));
3942
},
40-
[]
43+
[isMounted]
4144
);
4245

4346
const amendViewState = useCallback(

packages/core/src/useIsMounted.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
export const useIsMounted = () => {
4+
const mountedRef = useRef(false);
5+
useEffect(() => {
6+
mountedRef.current = true;
7+
return () => {
8+
mountedRef.current = false;
9+
};
10+
}, []);
11+
return mountedRef;
12+
};

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ To start using React Complex Tree, install it to your project as a dependency vi
2424

2525
```
2626
npm install react-complex-tree
27+
yarn add react-complex-tree
2728
```
2829

2930
then import it and add your tree structure with
@@ -87,3 +88,15 @@ More details at [the Get-Started Guide](https://rct.lukasbach.com/docs/getstarte
8788
- **Powered by React and TypeScript**
8889

8990
React Complex Tree is powered by React (duh) and is easily integrated in existing React projects by just importing and using the provided components. Comprehensive type information is given as TypeScript interfaces, that ease the integration and provide additional type safety, no matter whether you use TypeScript in your project or not.
91+
92+
93+
# Hints for contributing
94+
95+
If you want to develop RCT locally, here are some hints:
96+
97+
- Use [volta](https://volta.sh) to make sure you have a compatible NodeJS and Yarn version installed
98+
- Run `yarn` to install dependencies
99+
- Run `yarn build` once locally before running any dev commands
100+
- Run `yarn start` to start docusaurus and the package in watch mode, and/or `yarn storybook` to run storybook
101+
- Make sure to run linter and tests before pushing changes
102+
-

0 commit comments

Comments
 (0)