Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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
Javascript 对非组件JS文件使用引用_Javascript_Reactjs_Dom_Ref - Fatal编程技术网

Javascript 对非组件JS文件使用引用

Javascript 对非组件JS文件使用引用,javascript,reactjs,dom,ref,Javascript,Reactjs,Dom,Ref,我在JS非组件文件中有一个helper函数,我想在我的组件中导出和使用它。在这个函数中,我当前使用findDOMNode以传递给它的d3graph对象为目标,但这给出了一个警告,表明findDOMNode函数已被弃用。你能帮我重构这个以使用refs吗 请参见下面的代码: tick = (that) => { that.d3Graph = d3.select(ReactDOM.findDOMNode(that)); nsp.force.on('tick', () =>

我在JS非组件文件中有一个helper函数,我想在我的组件中导出和使用它。在这个函数中,我当前使用findDOMNode以传递给它的d3graph对象为目标,但这给出了一个警告,表明findDOMNode函数已被弃用。你能帮我重构这个以使用refs吗

请参见下面的代码:

tick = (that) => {
    that.d3Graph = d3.select(ReactDOM.findDOMNode(that));
    nsp.force.on('tick', () => {
      that.d3Graph.call(updateGraph)
    });
  };

您需要将ref传递给helper函数:

const tick = (ref) => {
  ref.current.d3Graph = d3.select(ref.current);
  nsp.force.on("tick", () => {
    ref.current.d3Graph.call(() => console.log("called"));
  });
};
使用
useRef
(函数组件)或
createRef
(类组件)获取ref可能

const graphRef=useRef();
useffect(()=>{
勾选(当前);
}, [])

您需要将ref传递给helper函数:

const tick = (ref) => {
  ref.current.d3Graph = d3.select(ref.current);
  nsp.force.on("tick", () => {
    ref.current.d3Graph.call(() => console.log("called"));
  });
};
使用
useRef
(函数组件)或
createRef
(类组件)获取ref可能

const graphRef=useRef();
useffect(()=>{
勾选(当前);
}, [])