mirror of
https://github.com/visgl/react-map-gl.git
synced 2026-01-18 15:54:22 +00:00
update state management article (#443)
This commit is contained in:
parent
ff384219ab
commit
fb3ea95dd5
@ -11,6 +11,41 @@ The most simple handling of this intent is to save it and pass it back to the co
|
||||
|
||||
User interaction and transition will not work without a valid `onViewportChange` handler.
|
||||
|
||||
The advantage of this practice is that it ensures a single source of truth regarding the viewport state (in the example above, saved in the `state` of the container component). When you use this viewport state to direct the rendering of other components, it is guaranteed that they will always be synced with the map.
|
||||
|
||||
You may apply additional constraints to the viewport:
|
||||
|
||||
```jsx
|
||||
_onViewportChange = viewport => {
|
||||
if (viewport.longitude > 0) {
|
||||
viewport.longitude = 0;
|
||||
}
|
||||
this.setState({viewport});
|
||||
}
|
||||
|
||||
render() {
|
||||
return <ReactMapGL {...this.state.viewport} onViewportChange={this._onViewportChange} />
|
||||
}
|
||||
```
|
||||
|
||||
Or manipulate the viewport outside of the ReactMap component:
|
||||
|
||||
```jsx
|
||||
_goToNYC = () => {
|
||||
const viewport = {...this.state, longitude: -74.1, latitude: 40.7};
|
||||
this.setState({viewport});
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<ReactMapGL {...this.state.viewport} onViewportChange={this._onViewportChange} />
|
||||
<button onClick={this._goToNYC}>New York City</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Using with Redux
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user