Skip to content

Instantly share code, notes, and snippets.

@stenin-nikita
Created June 2, 2020 07:13
Show Gist options
  • Save stenin-nikita/ef442abb51e3e8e15746fb46b1a59945 to your computer and use it in GitHub Desktop.
Save stenin-nikita/ef442abb51e3e8e15746fb46b1a59945 to your computer and use it in GitHub Desktop.
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