mapbox / mapbox-gl-directions

Directions plugin for mapbox-gl-js using Mapbox Directions API.
https://mapbox.com/mapbox-gl-js/example/mapbox-gl-directions/
ISC License
239 stars 127 forks source link

Rerender the mapbox on react state update #256

Open ajay-dev-j opened 4 years ago

ajay-dev-j commented 4 years ago

Library : react I need to toggle on/off the instructions by an external button.

const [toggleInstructions, setToggleInstructions] = useState(true);

`<button type="button" onClick={() => setToggleInstructions((prev) => !prev)}

Hide/Show `

const directions = new MapboxDirections({ accessToken: process.env.REACT_APP_MAPBOX_TOKEN, unit: 'metric', profile: 'mapbox/driving', interactive: false, controls: { inputs: false, profileSwitcher: false, instructions: toggleInstructions, }, });

map.on('load', () => { map.addControl(new mapboxgl.NavigationControl()); map.addControl(directions, 'top-left'); directions.setOrigin(start.address); wayPointsStatic.forEach((point, index) => { directions.addWaypoint(index, [point.lat, point.long]); // new mapboxgl.Marker().setLngLat([point.lat, point.long]).addTo(map); }); directions.setDestination(end.address); });

aasutossh commented 4 years ago

Seems like, this library doesn't work happily with React. Did you find a solution?

ajay-dev-j commented 4 years ago

No solution! My bad :(