我试图访问EditControl功能与editRef (const editRef = useRef();)以便以编程方式访问其处理程序。不幸的是,在web开发工具中出现了以下警告:"功能组件不能给出refs。尝试访问此ref将失败。你是想使用React.forwardRef()吗?";
因此,当我试图访问它时,我得到undefined
这是一个常见的警告,可以用forwardRef包装来解决,但在这种情况下,我不知道如何解决,因为我无法访问EditControl代码来添加ref.
是否有任何解决方案,以便我访问EditControl组件的对象?
<FeatureGroup>
<EditControl
ref={editRef}
onDrawVertex={checkOverlapping}
position='topright'
onCreated={pushPolyToArr}
draw={{
rectangle: false,
polyline: false,
circle: false,
marker: false,
circlemarker: false
/>
</FeatureGroup>
...
let rectangleRef = useRef();
useEffect(() => {
if (!rectangleRef.current || !isActiveSearchByArea) {
return;
}
rectangleRef.current._toolbars.draw._modes.rectangle.handler.enable();
}, [rectangleRef, isActiveSearchByArea]);
const onMountedRect = (e) => {
rectangleRef.current = e;
};
return (
<FeatureGroup>
<EditControl
onMounted={onMountedRect}
position='topright'
draw={{
rectangle: false,
polyline: false,
circle: false,
circlemarker: false,
marker: false,
polygon: false
}}
edit={{
edit: false,
remove: false
}}
/>
</FeatureGroup>
);