Javascript 对非组件JS文件使用引用
我在JS非组件文件中有一个helper函数,我想在我的组件中导出和使用它。在这个函数中,我当前使用findDOMNode以传递给它的d3graph对象为目标,但这给出了一个警告,表明findDOMNode函数已被弃用。你能帮我重构这个以使用refs吗 请参见下面的代码: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', () =>
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(()=>{
勾选(当前);
}, [])