Improve return type of useNodesdata
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { memo, useState } from 'react';
|
||||
import { Position, NodeProps, Handle, useReactFlow } from '@xyflow/react';
|
||||
import type { TextNode } from '.';
|
||||
|
||||
function TextNode({ id, data }: NodeProps) {
|
||||
function TextNode({ id, data }: NodeProps<TextNode>) {
|
||||
const { updateNodeData } = useReactFlow();
|
||||
const [text, setText] = useState(data.text);
|
||||
const updateText = (text: string) => {
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { memo, useEffect } from 'react';
|
||||
import { Position, NodeProps, useReactFlow, Handle, useNodeConnections, useNodesData } from '@xyflow/react';
|
||||
import { isTextNode, type TextNode, type MyNode } from '.';
|
||||
import { isTextNode, type TextNode, type MyNode, type UppercaseNode } from '.';
|
||||
|
||||
function UppercaseNode({ id }: NodeProps) {
|
||||
function UppercaseNode({ id }: NodeProps<UppercaseNode>) {
|
||||
const { updateNodeData } = useReactFlow();
|
||||
const connections = useNodeConnections({
|
||||
handleType: 'target',
|
||||
});
|
||||
const nodesData = useNodesData<MyNode>(connections[0]?.source);
|
||||
const textNode = isTextNode(nodesData) ? nodesData : null;
|
||||
|
||||
useEffect(() => {
|
||||
updateNodeData(id, { text: textNode?.data.text.toUpperCase() });
|
||||
}, [textNode]);
|
||||
const text = nodesData?.type === 'text' ? nodesData?.data.text.toUpperCase() : undefined;
|
||||
updateNodeData(id, { text });
|
||||
}, [nodesData]);
|
||||
|
||||
return (
|
||||
<div style={{ background: '#eee', color: '#222', padding: 10, fontSize: 12, borderRadius: 10 }}>
|
||||
|
||||
Reference in New Issue
Block a user