gribnoysup / react-yandex-maps

Yandex Maps API bindings for React
MIT License
327 stars 116 forks source link

ability switch language (and other api props) dynamically #204

Closed ValeryVS closed 4 years ago

ValeryVS commented 4 years ago

Now Provider doesn't update script even if another lang will be passed to YMaps.

  <YMaps
    query={{
      lang: language
    }}
  >

It would be good if it will be able to reload api with different language, like there https://tech.yandex.ru/maps/jsbox/2.1/language

ValeryVS commented 4 years ago

Is it possible with two providers? I tried to do some research, and even be able to reload map, but it's language isn't changed. https://codesandbox.io/s/react-yandex-maps-vector-experiment-nvmgb

ValeryVS commented 4 years ago

https://github.com/gribnoysup/react-yandex-maps/blob/697996e34b920b3de628015954cf87e11d372cbb/src/YMaps.js#L34-L38 here returned api from first loaded provider

ValeryVS commented 4 years ago

Made some further research. Seems like Provider should be able to reload ymaps and reset context, while all components should be able to update itself, when context is updated.

gribnoysup commented 4 years ago

I'm not sure that I want to support it in the library to be honest, but you can force React to completely rerender the Provider component with the key prop and this will force the re-fetching of the API with the new locale. Here's a code example of this approach:

function App() {
  const [lang, setLang] = React.useState("ru_RU");

  return (
    <>
      <button onClick={() => setLang("en_US")}>Toggle language</button>
      <YMaps key={lang} query={{ lang }}>
        <Map defaultState={{ center: [0, 0], zoom: 5 }} />
      </YMaps>
    </>
  );
}

Edit vigorous-forest-t9keu