gpt4 book ai didi

reactjs - react useState 选项值落后于 onChange 选择 1 步

转载 作者:行者123 更新时间:2023-12-05 05:06:57 26 4
gpt4 key购买 nike

size.value _updateData 时状态是后面的一个选项函数触发 <select> 的 onChange .例如,假设 <select> 的起始默认值选项是“全部”,locations为空...选项更改为“中”,locations是“全部”...选项更改为“大”,locations是“中等”...等等。最初我想要 _updateData选择“全部”运行 onload 的函数也不起作用,它会抛出错误 Cannot read property 'target' of undefined on 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;

最佳答案

是的,您正在安装 useEffect Hook 中调用您选择的 onChange 处理程序,没有事件对象来取消引用 target 属性。我会分解出 updateData 代码的其余部分,以便您可以使用初始状态值调用它。这将允许您使用初始大小状态日期更新安装位置详细信息AND 选择的 onChange 将保持原样。 p>

注意:您应该注意状态更新直到下一个渲染周期才会生效,因此在您的代码中您使用新值调用 setSize 但继续处理位置当前大小值,所以你需要转发当前值。

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"}) // initial size state here

useEffect(() => {
setLocations(geodata);
updateLocationData(size.value); // call the location updater on mount with the initial size state value
}, []);

const _updateViewport = viewport => {
setViewport(viewport)
}

const _updateData = event => {
setSize({value: event.target.value})
updateLocationData(event.target.value); // forward current size value
}

const updateLocationData = (sizeValue) => { // forwarded size value
const tempLocations = [];
locations.forEach(function(res) {
if (sizeValue === "All") { // forwarded size value for comparison
tempLocations.push(res);
} else if (res.Size === sizeValue) { // forwarded size value for comparison
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.value}> // need to unpack the actual size value
<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;

关于reactjs - react useState 选项值落后于 onChange 选择 1 步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com