R
R
RokeAlvo2019-08-04 22:38:20
Yandex maps
RokeAlvo, 2019-08-04 22:38:20

How to hang a click handler in react-yandex-maps?

why is this code not working:

export default function({center, clickOnMap}) {
  return (
    <YMaps query={{
      apikey: 'my_api_key',
    }}>
      <div>
        <Map
             className='map-container'
             state={{center: center, zoom: 16}}
             onLoad={(inst)=>{return inst.events.add('click', clickOnMap)}}
        >
          <Placemark geometry={center} />

        </Map>
      </div>
    </YMaps>
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-04
@RokeAlvo

why is this code not working


onLoad={(inst)=>{return inst.events.add('click', clickOnMap)}}


Because through onLoad you are accessing the ymaps api and not the map instance.
Want an instance of the map - use instanceRef:
instanceRef={inst => inst.events.add('click', clickOnMap)}

But in general, in order to assign a click handler, the map instance does not need to be touched:
onClick={clickOnMap}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question