@@ -106,8 +106,8 @@ Input chips are a variant of chips which enable user input by converting text in
106
106
class MyInputChips extends React .Component {
107
107
state = {
108
108
chips: [
109
- {label: ' Jane Smith' , id: ' janesmith ' },
110
- {label: ' John Doe' , id: ' johndoe ' }
109
+ {label: ' Jane Smith' , id: ' JaneSmith ' },
110
+ {label: ' John Doe' , id: ' JohnDoe ' },
111
111
],
112
112
};
113
113
@@ -119,11 +119,15 @@ class MyInputChips extends React.Component {
119
119
// Create a new chips array to ensure that a re-render occurs.
120
120
// See: https://reactjs.org/docs/state-and-lifecycle.html#do-not-modify-state-directly
121
121
const chips = [... this .state .chips ];
122
- chips .push ({label, id});
123
- this .setState ({chips});
124
- e .target .value = ' ' ;
122
+ if (chips .some ((v ) => v .id === id)) {
123
+ console .error (' There is already a chip which has same key.' );
124
+ } else {
125
+ chips .push ({label, id});
126
+ this .setState ({chips});
127
+ e .target .value = ' ' ;
128
+ }
125
129
}
126
- }
130
+ };
127
131
128
132
render () {
129
133
return (
@@ -135,6 +139,7 @@ class MyInputChips extends React.Component {
135
139
>
136
140
{this .state .chips .map ((chip ) =>
137
141
< Chip
142
+ id= {chip .id }
138
143
key= {chip .id } // The chip's key cannot be its index, because its index may change.
139
144
label= {chip .label }
140
145
trailingIcon= {< MaterialIcon icon= ' cancel' / > }
0 commit comments