1. What is it?
First, give react
an example of list rendering in components:
const data = [
{ id: 0, name: 'abc' },
{ id: 1, name: 'def' },
{ id: 2, name: 'ghi' },
{ id: 3, name: 'jkl' }
];
const ListItem = (props) => {
return <li>{props.name}</li>;
};
const List = () => {
return (
<ul>
{data.map((item) => (
<ListItem name={item.name}></ListItem>
))}
</ul>
);
};
react
Then you can see the prompted warning message in the output :
Each child in a list should have a unique "key" prop.
According to the meaning, each child element of the rendering list should need a unique key
value
Here you can use id
the attribute of the list as key
the value to solve the above warning
const List = () => {
return (
<ul>
{data.map((item) => (
<ListItem name={item.name} key={item.id}></ListItem>
))}
</ul>
);
};
Two, the role
Like Vue
the same, React
there is also Diff
an algorithm, and key
the function of the element attribute is to determine whether the element is a newly created or moved element, thereby reducing unnecessary element rendering
Therefore key
, the value needs to assign a certain identity to each element
If a piece of data is inserted after the data in the rendering of the list data, key
the effect is not significant, as follows:
this.state = {
numbers:[111,222,333]
}
insertMovie() {
const newMovies = [...this.state.numbers, 444];
this.setState({
movies: newMovies
})
}
<ul>
{
this.state.movies.map((item, index) => {
return <li>{item}</li>
})
}
</ul>
The previous elements are in diff
the algorithm. Since the previous elements are exactly the same, no deletion and creation operations will be generated. In the last comparison, they need to be inserted into a new DOM
tree
Therefore, in this case, key
it doesn't make much sense whether the element has attributes
key
Let's take a look at the difference between using and not using when inserting data in the front key
:
insertMovie() {
const newMovies = [000 ,...this.state.numbers];
this.setState({
movies: newMovies
})
}
When you have it key
, match the sub-elements on the original tree and the sub-elements on the latest tree react
according to the attributes. In the above case, you only need to insert the 000 element to the front positionkey
When there key
is none, all li
tags need to be modified
Similarly, it does not mean that having key
a value means higher performance. If only the content of the text has changed, key
the performance and efficiency will be higher if it is not written.
The main reason is that not writing key
is to replace all the text content, and the nodes will not change
Writing key
involves the addition and deletion of nodes. If it is found that the old one key
does not exist, it will be deleted, and if the new one key
does not exist before, it will be inserted, which increases performance overhead.
3. Summary
Good use of key
attributes is a very critical step in performance optimization. The precautions are:
-
key should be unique
-
Do not use random values for the key (the random number will regenerate a number in the next render)
-
Using index as the key value does not optimize performance
react
The judgment key
process is as follows: