Skip to content

Draw Polygon how to call the mapbox-gl-draw Methods? #2053

Answered by joel-daros
joel-daros asked this question in Q&A
Discussion options

You must be logged in to vote

I think I found a solution combine forwardRef and useImperativeHandle to solve:

export const DrawControl = React.forwardRef((props: DrawControlProps, ref) => {
  const drawRef = useControl<MapboxDraw>(
    () => new MapboxDraw(props),
    ({ map }) => {
      map.on("draw.create", props.onCreate);
      map.on("draw.update", props.onUpdate);
      map.on("draw.delete", props.onDelete);
      map.on("draw.modechange", props.onModeChange);
    },
    ({ map }) => {
      map.off("draw.create", props.onCreate);
      map.off("draw.update", props.onUpdate);
      map.off("draw.delete", props.onDelete);
      map.off("draw.modechange", props.onModeChange);
    },
    {
      position: props.po…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@tordans
Comment options

@tordans
Comment options

@felixerdy
Comment options

@joel-daros
Comment options

@SuhaibMaraqa
Comment options

Answer selected by joel-daros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants