Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在Typescript中声明React组件扩展包组件?_Reactjs_Typescript - Fatal编程技术网

Reactjs 如何在Typescript中声明React组件扩展包组件?

Reactjs 如何在Typescript中声明React组件扩展包组件?,reactjs,typescript,Reactjs,Typescript,我正在寻找一种方法来声明一个React组件,用我自己在Typescript中的道具扩展现有组件。源组件是来自react-d3-treenpm包v2.0.0的react3tree。因此,我: import ReactD3Tree from "react-d3-tree"; import { TreeProps } from "react-d3-tree/lib/Tree/types"; import { RawNodeDatum } from "r

我正在寻找一种方法来声明一个React组件,用我自己在Typescript中的道具扩展现有组件。源组件是来自
react-d3-tree
npm包v2.0.0的react3tree。因此,我:

import ReactD3Tree from "react-d3-tree";
import { TreeProps } from "react-d3-tree/lib/Tree/types";
import { RawNodeDatum } from "react-d3-tree/lib/types/common";

interface MyNode extends RawNodeDatum {
   // some properties
}

interface MyProps extends TreeProps {
   onNodeClick: (targetNode: MyNode, event: Event) => any;
}

let MyTree: React.ComponentClass<MyProps>;


const MyComponentWithOtherOperations = () => {
    ...some more code here enter code here

    return <MyTree data={data} onNodeClick={handleNodeClick} />
}
从“react-d3-tree”导入react3-tree;
从“react-d3-tree/lib/tree/types”导入{TreeProps};
从“react-d3-tree/lib/types/common”导入{RawNodeDatum};
接口MyNode扩展了RawNodeDatum{
//一些性质
}
接口MyProps扩展了TreeProps{
onNodeClick:(targetNode:MyNode,event:event)=>any;
}
让MyTree:React.ComponentClass;
const mycomponent with otheroperations=()=>{
…此处有更多代码在此处输入代码
返回
}

我的问题是如何正确地覆盖接受
targetNode:RawNodeDatum
作为参数的
react3tree
onNodeClick
属性?我想将
targetNode:MyNode
作为参数,其中
MyNode
扩展
RaNodeDatum
提前感谢如果我理解正确,您希望将一个额外的参数传递给您的NodeClick事件处理程序。你要找的是一个咖喱函数

const nodeClickHandler = customprop => nodeValue => {
     console.log(customprop, nodeValue); 
};

const MyComponentWithOtherOperations = () => {
    ...some more code here enter code here

    return <MyTree data={data} onNodeClick={nodeClickHandler("your custom data")} /> }
const nodeClickHandler=customprop=>nodeValue=>{
console.log(customprop、nodeValue);
};
const mycomponent with otheroperations=()=>{
…此处有更多代码在此处输入代码
返回}

如果我理解正确,您希望向NodeClick事件处理程序传递一个额外的参数。你要找的是一个咖喱函数

const nodeClickHandler = customprop => nodeValue => {
     console.log(customprop, nodeValue); 
};

const MyComponentWithOtherOperations = () => {
    ...some more code here enter code here

    return <MyTree data={data} onNodeClick={nodeClickHandler("your custom data")} /> }
const nodeClickHandler=customprop=>nodeValue=>{
console.log(customprop、nodeValue);
};
const mycomponent with otheroperations=()=>{
…此处有更多代码在此处输入代码
返回}

您计划如何使用
ReactD3Tree
?@rameshredy我想覆盖
onNodeClick
方法,该方法接受节点作为参数。我想在那里设置自定义节点。TypeScript支持多级继承。您可以扩展React3树,该树扩展React.Component。这种方法有问题吗@我明白你的意思了。请参阅-,了解。您计划如何使用
ReactD3Tree
?@ramesthredy我想覆盖
onNodeClick
方法,该方法接受节点作为参数。我想在那里设置自定义节点。TypeScript支持多级继承。您可以扩展React3树,该树扩展React.Component。这种方法有问题吗@我明白你的意思了。有关详细信息,请参见-。