Skip to content

Instantly share code, notes, and snippets.

@dineshdeveloper1
Last active July 22, 2022 04:26
Show Gist options
  • Save dineshdeveloper1/41b6f5cf955ce3f9ffb9eb55c4276a83 to your computer and use it in GitHub Desktop.
Save dineshdeveloper1/41b6f5cf955ce3f9ffb9eb55c4276a83 to your computer and use it in GitHub Desktop.
// static list ==========================================================
class MyList extends React.Component{
render(){
return(
<>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
</ul>
</>
)
}
}
// rendarling a list dynamically ==========================================================
(each child in a list should have a unique "key")
class MyList extends React.Component{
constructor(props){
super(props)
this.state = {
listitems : ['list item 1', 'list item 2', 'list item 3', 'list item 4']
}
}
render(){
return(
<>
<ul>
{this.state.listitems.map( (list) => (
<li key={list}>{list}</li>
))}
</ul>
</>
)
}
}
// rendering and array of objects as a list ==========================================================
class MyList extends React.Component{
constructor(props){
super(props)
this.state = {
productLists : [
{
id : 1,
productname : 'Apple iPhone XR',
productprice : 'Rs. 42,000'
},
{
id : 2,
productname : 'Apple iPhone 11',
productprice : 'Rs. 54,000'
},
{
id : 3,
productname : 'Apple iPhone 12',
productprice : 'Rs. 94,000'
}
]
}
}
render(){
return(
<>
<ul>
{this.state.productLists.map( (list) => (
<li key={list.id}>{list.id} --- {list.productname}, {list.productprice} </li>
))}
</ul>
</>
)
}
}
// map() function take an array of number and double their values, assigned to a new array =====================================================
class MyList extends React.Component{
render(){
let singleList = [1, 2, 3, 4, 5]
let doubleList = singleList.map( (i) => i * 2 )
return(
<>
<h1>Single List : {singleList}</h1>
<h1>Double List : {doubleList}</h1>
</>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment