简体   繁体   English

如何使用react为状态初始化的Map添加值?

[英]How to add value to Map initialized in state using react?

I have application and i need to keep search history, in my opinion Map is suitable here:我有应用程序,我需要保留搜索历史,我认为Map适合这里:

const [searchHistory, setSearchHistory] = useState(new Map());

to update it use setSearchHistory(new Map([[text, result.repos]]));更新它使用setSearchHistory(new Map([[text, result.repos]])); but it replaces Map with new Map, could you please tell me how can i add value to Map initialized in state ?但它用新的 Map 替换了 Map,你能告诉我如何为状态初始化的 Map 添加值吗?

State updater function returned by useState doesn't merges the state like this.setState() in class-based components. useState返回的状态更新器函数不会像基于类的组件中的this.setState()那样合并状态。 In functional components, you have to manually merge the old state with the new state.在功能组件中,您必须手动将旧状态与新状态合并。

You are passing the new Map object to setSearchHistory , so it will completely overwrite the old state.您将新的Map对象传递给setSearchHistory ,因此它将完全覆盖旧状态。

You could update the state by first copying the entries in the old Map object in to new Map object, then add the new entry you want to add in the state.您可以通过首先将旧Map对象中的条目复制到新Map对象中,然后添加要添加到状态中的新条目来更新状态。 Finally, pass this new Map object to setSearchHistory .最后,将这个新的Map对象传递给setSearchHistory

// copy (shallow) the old map's entries in the new Map
const updateMap = new Map(searchHistory);  
// add the new entry in the 'updatedMap'
updatedMap.set(key, value);
// update the state
setSearchHistory(updatedMap);

You can achieve that like the following:您可以通过以下方式实现:

const [searchHistory, setSearchHistory] = useState(new Map());

function onChange(key, value) {
  // change the searchHistory map with a new Map from the current searchHistory, and the new key, value
  setSearchHistory(new Map([...searchHistory, [key, value]]));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在React状态内向数组添加值 - How to add a value to an array inside a React state 我如何更新状态以使用 array.map 函数更新数组内对象的值 - how do i update state in react for updating the value of an object inside an array using array.map function 如何存储一对键和值,并使用动作动态添加它们? 映射导致“检测到不可序列化状态”错误 - How to store a pairs of key and value, and add them dynamically using action? Map cause 'detected unserializable state' error 反应:如何将选择选项值映射到状态键? - React: How to map select option value to a state key? 如何推送到 map 值数组并将其用作 React 中的 state? - How to push to a map value array and use it as state in React? 反应:如何使用 leaflet 通过对 state 的 fetch 调用来 map 一个 geojson? - React: How to map a geojson via fetch call to state using leaflet? 当使用地图函数中的状态在 React JS 中更改一个项目值时,如何动态更新购物车中的值? - How to dynamically update values in Shopping Cart when one item value is changing in React JS using state in map function? 如何将新的子键值添加到 React State 对象 - How to add new child key-value to a React State object 如何在 React.js 中将输入值添加到 state? - how can i add input value to state in React.js? 如何在 React 中使用 state 值动态地将新属性添加到文件 object - How to add new Properties to File object in React with state value dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM