Created
October 23, 2019 09:00
-
-
Save react-ram/47c6c7809a4860e6831e2f19b6791491 to your computer and use it in GitHub Desktop.
Basics - Lists & keys - correct usage of key. example 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
export class App extends Component { | |
render() { | |
const numbers = [1, 2, 3, 4, 5]; | |
return ( | |
<div> | |
<NumbersList numbers={numbers} /> | |
</div> | |
); | |
} | |
} | |
function NumbersList(props) { | |
const numbers = props.numbers; | |
const listItems = numbers.map(number => ( | |
<ListItem key={number.toString()} value={number} /> //use key only where map function is used | |
)); | |
return <ul>{listItems}</ul>; | |
} | |
function ListItem(props) { | |
const value = props.value; | |
return <li>{value}</li>; // dont use key here | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment