当函数size.value
触发. 例如,假设开始的 defaultValue 选项为“All”,为空...该选项更改为“Medium”,为“All”...该选项更改为“Large”,为“Medium”。 ..等等。最初我希望该函数在选择“All”的情况下运行 onload 也不起作用,它会引发错误 Cannot read property 'target' of undefined on 。我在这里做错了什么?谢谢您的帮助。_updateData
<select>
<select>
locations
locations
locations
_updateData
setSize({value: event.target.value})
const Map = () => {
const [viewport, setViewport] = useState({longitude: -98.58, latitude: 39.83, zoom: 3.5})
const [locations, setLocations] = useState([])
const [geojson, setGeojson] = useState(null)
const [size, setSize] = useState({value: "All"})
useEffect(() => {
setLocations(geodata)
_updateData()
}, []);
const _updateViewport = viewport => {
setViewport(viewport)
}
const _updateData = event => {
setSize({value: event.target.value})
const tempLocations = [];
locations.forEach(function(res) {
if (size.value === "All") {
tempLocations.push(res);
} else if (res.Size === size.value) {
tempLocations.push(res);
}
});
var data = {
...
};
setGeojson(data);
}
return (
<ReactMapGL
{...viewport}
onViewportChange={_updateViewport}
width="100%"
height="100%"
mapStyle={mapStyle}
mapboxApiAccessToken={TOKEN}>
<Source id="my-data" type="geojson" data={geojson}>
<Layer {...icon} />
</Source>
<div style={navStyle}>
<NavigationControl onViewportChange={_updateViewport} />
<select onChange={_updateData} defaultValue={size}>
<option value="All">All</option>
<option value="Large">Large</option>
<option value="Medium">Medium</option>
<option value="Small">Small</option>
<option value="Very Small">Very Small</option>
</select>
</div>
</ReactMapGL>
);
}
export default Map;