代码之家  ›  专栏  ›  技术社区  ›  andreask

如何从第三方DOM库访问对象的属性,该库用于从外部访问反应组件

  •  0
  • andreask  · 技术社区  · 7 年前

    我想在一个基于反应的项目中使用vis.js。由于所有VIS网络实现对我来说都不起作用,所以我必须使用普通库。

    这是我的测试反应组件

    import dataset,network from'vis';
    从“react”导入react,component,createRef;
    
    类VisNetwork扩展组件{
    
    构造()
    超级();
    
    this.network=
    this.appif=createRef();
    this.nodes=新数据集([
    id:1,label:'节点1',
    id:2,label:'节点2',
    id:3,label:'节点3',
    ID:4,标签:“节点4”,
    ID:5,标签:“节点5”
    )
    this.edges=新数据集([
    从:1到:3,
    从:1到:2,
    从:2到:4,
    从:2到:5_
    )
    数据={
    节点:this.nodes,
    边:this.edges
    };
    this.options=
    }
    
    组件didmount()。{
    this.network=新网络(this.appif.current,this.data,this.options);
    }
    
    RelDead(){
    返回(
    <DIV REF=this.appif/>
    ;
    }
    }
    
    导出默认visnetwork;
    < /代码> 
    
    

    它是迄今为止唯一安装的组件

    reactdom.render(<visnetwork/>,document.getElementByID('myNetwork'));
    < /代码> 
    
    

    我的问题是如何访问网络的属性,例如,获取或删除节点。

    node=nodes.get(nodeid);
    < /代码> 
    
    

    我读到了关于react-ref的文章,尝试了 ()=>console.log(document.getElementsByClassName('vis-network')).as callback ofreactdom.render().but that does't help.

    另一个问题是为什么没有设置REF,它只是<div>

    因为我认为组件的构造函数中的this.apprf=createref();ref=this.apprfin render()将导致引用。

    我希望你能给我一个提示。

    import { DataSet, Network } from 'vis';
    import React, { Component, createRef } from "react";
    
    class VisNetwork extends Component {
    
        constructor() {
            super();
    
            this.network = {};
            this.appRef = createRef();
            this.nodes = new DataSet([
                { id: 1, label: 'Node 1' },
                { id: 2, label: 'Node 2' },
                { id: 3, label: 'Node 3' },
                { id: 4, label: 'Node 4' },
                { id: 5, label: 'Node 5' }
            ]);
            this.edges = new DataSet([
                { from: 1, to: 3 },
                { from: 1, to: 2 },
                { from: 2, to: 4 },
                { from: 2, to: 5 }
            ]);
            this.data = {
                nodes: this.nodes,
                edges: this.edges
            };
            this.options ={};
        }
    
        componentDidMount() {
            this.network = new Network(this.appRef.current, this.data, this.options);
        }
    
        render() {
            return (
                <div ref={this.appRef} />
            );
        }
    }
    
    export default VisNetwork;
    

    它是目前唯一安装的组件

    ReactDOM.render(<VisNetwork />,document.getElementById('mynetwork'));
    

    我的问题是如何访问网络的属性,例如,获取或删除节点。

    node = nodes.get(nodeId);
    

    我读过关于反应裁判的文章,尝试过 () =>{ console.log(document.getElementsByClassName('vis-network'))作为回调ReactDOM.render()但这没有帮助。

    另一个问题是为什么裁判没有设定,只是<div>.

    因为我认为this.appRef = createRef();在组件的构造函数中,ref={this.appRef}在render()中,将导致引用。

    Screenshot of debugger

    希望你能给我个提示。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jonas Wilms    7 年前

    实际上,流程应该是相反的,定义组件外部的节点,然后将它们传入。为此,将构造函数定义为:

     constructor(props) {
        super(props);
        this.nodes = props.nodes;
        //...
    }
    

    然后将其构造为:

     const nodes = new DataSet([/*...*/]);
     ReactDOM.render(<VisNetwork nodes={nodes} />,document.getElementById('mynetwork'));
    

    注意你应该把任何有状态的东西 this.state 使用 setState 改变它。