简体   繁体   中英

React key prop not must

我对 PROP 键有误解 我管理一个带有删除选项的列表 我不使用 KEY(或有​​时将索引名称用作键) 尽管我预计会发生问题,但一切正常

The key prop is used by React internally to track a Component back to its respective virtual DOM element. This helps React do minimal component re-renders, which in turn makes your application more performant. From the React docs on keys :

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys

The key prop is not required, but using it with a list will help to significantly reduce the number of times a Component list child is re-rendered.

TL;DR : Always use a unique key for children of a list (eg, when using map over an array to create components). It'll help React, but isn't necessary for your app to function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM