reactjs - 如何在使用地图功能时更新一个特定元素的状态
问题描述
我需要针对这种情况的解决方案,我将对此进行解释。
设想:
1-我有一张地图,onMoveEnd
我更新了位置state
;
2-我有一个列表,我正在循环使用.map
并渲染SampleComponent
。
3-然后将位置传递给SampleComponent
;
4-我SampleComponent
在Expansion Panel
里面,我用它来切换扩展面板isExpanded
来处理打开和关闭操作;
问题:
每次更新位置时,它都会在SampleComponent
我循环的所有内容中更新。但我只需要在SampleComponent
其中更新它isExpanded = true
,而不是全部更新它。
任何解决方案?
解决方案
实际上我UNSAFE_componentWillReceiveProps
以前曾经处理过这种情况。
但UNSAFE_
对我来说看起来很丑所以我这样做:
在我SampleComponent
用过的
const [location, setlocation] = useState({ title: '' });
useEffect(() => {
if (props.isExpanded) {
setlocation(props.location);
}
}, [props.location.title]);
首先,我在需要更新位置时听取位置更改,我检查了isExpanded
以确保我的面板已打开,然后我应用更改setlocation(props.location)
。并将其更改props.location
为 local location
。
推荐阅读
- microsoft-graph-api - 为多个用户订阅 MS Graph API 中的存在
- python - 如何在最近的 Windows 计算机上的 C:\Program Files 中安装 Python?
- r - r Shiny:如何在应用程序上放置可下载的 PDF
- javascript - 我的下拉按钮在文本上不可点击,但可在按钮上的其他任何地方点击
- javascript - Promises:如何返回具有其他错误处理程序的 Promise
- jsonschema - json-schema 具有类型 x 的附加属性和至少一个
- r - 在控制台与 R 降价单元中运行时调度不同的打印方法?
- exchange-server - 如何创建基于域的 Exchange 2013/16 邮件流规则
- msix - 如何从命令行构建 MSIX
- laravel - 如何向具有不同会员角色的会员发送通知?