1 | # `createKeyFrom` Property on Column Definition
|
2 |
|
3 | There is a lot to talk about when we're talking about performance, especially when it comes to the React API. To fully understand the use and purpose of `createKeyFrom`, I think it's important to have an understanding of one of React's core tenets:
|
4 |
|
5 | ### Reconciliation
|
6 |
|
7 | And rather than fumble over the concept myself, I will simply link to React's own documentation https://facebook.github.io/react/docs/reconciliation.html
|
8 |
|
9 | This snippet, taken from the React documentation explains why the grid column definition allows for a single `createKeyFrom`:
|
10 |
|
11 | >
|
12 | Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many nodes to be unnecessarily re-created, which can cause performance degradation and lost state in child components.
|
13 | >
|
14 |
|
15 | When you set a column as `createKeyFrom`, you will be setting the `key` of the `Row` component, as the value at this `dataIndex`. Take the following HTML rendered by the grid component, with the following column definition:
|
16 |
|
17 | ````js
|
18 | const columns = [
|
19 | {
|
20 | name: 'Name',
|
21 | dataIndex: 'name',
|
22 | createKeyFrom: true
|
23 | },
|
24 | {
|
25 | name: 'Email',
|
26 | dataIndex: 'email'
|
27 | }
|
28 | ];
|
29 |
|
30 | ````
|
31 |
|
32 | ````html
|
33 | <tbody>
|
34 | <tr class="react-grid-row">
|
35 | <td class="react-grid-cell">
|
36 | <td class="react-grid-cell"><span>Michael</span></td>
|
37 | <td class="react-grid-cell"><span>michael@gmail.com</span></td>
|
38 | </td>
|
39 | </tr>
|
40 | <tr class="react-grid-row">
|
41 | <td class="react-grid-cell">
|
42 | <td class="react-grid-cell"><span>Allen</span></td>
|
43 | <td class="react-grid-cell"><span>allen@gmail.com</span></td>
|
44 | </td>
|
45 | </tr>
|
46 | </tbody>
|
47 | ````
|
48 | When the preceding rows get rendered with the above column definition, the resulting keys will look like:
|
49 |
|
50 | ````js
|
51 | const keys = ['Michael', 'Allen'];
|
52 | ````
|
53 |
|
54 | Provided **that these keys will be unique**, this will allow for the React reconciliation step will be optimized for performance. This is especially helpful when dealing with big data sets (> ~1000 rows).
|
55 |
|
56 | ### Take Aways
|
57 |
|
58 | 1. Only set a column as `createKeyFrom` if you can guarantee that the value for each row will be unique.
|
59 | 2. You can only set one column as `createKeyFrom`.
|
60 | 3. If you do not set a column as `createKeyFrom`, the row `index` will be used as the `key`.
|
61 |
|
62 |
|
\ | No newline at end of file |