Created
June 2, 2020 07:13
-
-
Save stenin-nikita/ef442abb51e3e8e15746fb46b1a59945 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { FC } from 'react'; | |
import { useAtom } from '@reatom/react'; | |
import { declareAtom } from '@reatom/core'; | |
interface TreeNode { | |
id: string; | |
parentId: string | null; | |
name: string; | |
children: string[]; | |
properties: { | |
disabled?: boolean; | |
}; | |
} | |
const treeItemsAtom = declareAtom<Record<string, TreeNode>>({}, on => []); | |
interface RendererProps { | |
id: string; | |
} | |
const Renderer: FC<RendererProps> = props => { | |
const { id } = props; | |
const node = useAtom(treeItemsAtom, state => state[id], [id]); | |
return <TreeItem node={node} />; | |
}; | |
interface TreeItemProps { | |
node: TreeNode; | |
} | |
const TreeItem: FC<TreeItemProps> = props => { | |
const { node } = props; | |
const { disabled } = useAtom( | |
treeItemsAtom, | |
state => { | |
return node.parentId ? state[node.parentId].properties : {}; | |
// ^^^ - ошибка условно здесь, так как после удаления данных уже нет | |
}, | |
[node.parentId] | |
); | |
return ( | |
<> | |
<div>{node.name}</div> | |
{!disabled && node.children.map(child => <Renderer key={child} id={child} />)} | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment