Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Изменение SearchControl при выборе точки на карте #249

Closed
madear opened this issue Jul 7, 2020 · 1 comment
Labels
question yandex.maps api Issues or questions related to Yandex.Maps, not to the library

Comments

@madear
Copy link

madear commented Jul 7, 2020

Здравствуйте,
можно ли изменять значение в searchcontrol при выборе точки на карте кликом?

@mmarkelov
Copy link
Contributor

@madear почему нет?

import React, { useState, useRef, useEffect } from "react";
import ReactDOM from "react-dom";
import {
  YMaps,
  Map,
  SearchControl,
  Placemark
} from "react-yandex-maps";

import "./styles.css";

const mapState = { center: [55.750625, 37.626], zoom: 7 };

function App() {
  // храним текущий текст
  const [text, setText] = useState(null);
  // ref searchcontrol
  const searchRef = useRef(null);

  useEffect(() => {
    if (text && searchRef.current) {
      // Меняем текст searchcontrol при изменении text
      searchRef.current.search(text);
    }
  }, [text]);

  return (
    <div className="App">
      <YMaps>
        <Map>
          <SearchControl
            instanceRef={ref => {
              if (ref) searchRef.current = ref;
            }}
          />
          <Placemark
            geometry={[55.684758, 37.738521]}
            properties={{
              balloonContentBody: "Test 1"
            }}
            // инициализируем изменение текста при клике на метку
            onClick={() => setText("Test 1")}
          />
          <Placemark
            geometry={[55.254758, 37.538521]}
            properties={{
              balloonContentBody: "Test 2"
            }}
            onClick={() => setText("Test 2")}
          />
        </Map>
      </YMaps>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

@mmarkelov mmarkelov added question yandex.maps api Issues or questions related to Yandex.Maps, not to the library labels Jul 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question yandex.maps api Issues or questions related to Yandex.Maps, not to the library
Projects
None yet
Development

No branches or pull requests

2 participants