Professional Documents
Culture Documents
is mutable
step 1:
step 2:
state vs props:
================
this.setState({a:9},()=>{console.log('hi')})
step 4: assign keys using index and then using user defined id
and
in iteration
<div className="App">
{this.state.monsters.map((mon,i)=><h1>{mon.id} {mon.name}</h1>)}
</div>
or
<div className="App">
{this.state.monsters.map((mon)=><h1 key={mon.id}>{mon.id} {mon.name}</h1>)}
</div>
step 5: life cycle basic method and external server or api endpoints
Step 6:
in card-list.js
================
const cardList=(props)=>{
console.log(props); //to know what is props
return (<div>hello</div>);
}
in app.js:
============
<div className="App">
<CardList/>
</div>
step 7:
pass the api data to card-list component thru props and move the iteration code to
it
in app.js:
===========
<CardList>
{this.state.heros.map((mon,i)=><h1 key={mon.id}>{mon.id} {mon.name}</h1>)}
</CardList>
in card-list.js
=================
const cardList=(props)=>{
console.log(props); //to kknow what is props
return (<div className='card-list'>{props.children}</div>);
}
.card-list{
width:85vw;
margin:0 auto;
display : grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-gap: 20px;
}
step 8: props drilling - pass the data heros to card component which will be sub
component of card-list component
create a folder card and create a js file with name card.js & card.scss too
in card.js
===========
const Card=(props)=>{
console.log(props); //to kknow what is props
return (<div >{props.hero.id}{props.hero.name}</div>);
}
<div className='card-list'>
{
this.props.data.map((mon)=>( <Card key={mon.id} hero={mon}/>))
}
</div>
);
}
}
<CardList data={this.state.heros}/>
</div>
);
.card-container {
display: flex;
flex-direction: column;
background-color: #95dada;
border: 1px solid grey;
border-radius: 5px;
padding: 25px;
cursor: pointer;
-moz-osx-font-smoothing: grayscale;
backface-visibility: hidden;
transform: translateZ(0);
transition: transform 0.25s ease-out;
}
.card-container:hover {
transform: scale(1.05);
}
it depends on
in App.js:
============
Change the above line as shown below to get value of char being typed
assign the value typed to state variable, but before create a variable
Now create 2 temp variables one for filtered array and other for search value
inside render()
reason to create in render is every change of search field which is state var,
calls render
so that filter can happen and display the matched char list
const
filteredHero=this.state.heros.filter(m=>m.name.toLowerCase().includes(this.state.se
archFieldData.toLowerCase()));
and
<CardList data={filteredHero}/>
deploy in GIT :
===================================================================================
=====================
to show react virtual dom and its working from our hero app
do some modification
create a title variable, and set the state in on change event in search box, inside
setstate
this.state={
heros:[
{name:'IronMan',id:1234},
{name:'SpiderMan',id:1334},
{name:'Captain America',id:2335},
{name:'Hulk',id:9264}
],
searchFieldData:'',
title:''
}
<h1> {this.state.title}</h1>
setState is async,
constructor(){
super();
this.state={
status:''
}
}
this.setState((prevState,prevProps)={console.log(prevState.status)},
()=>{console.log('callback '+this.state.status)})