@ -2,25 +2,105 @@ import React, { Component } from 'react';
export class Home extends Component {
static displayName = Home . name ;
state = {
people : [ ]
}
componentDidMount = ( ) => {
fetch ( '/people' )
. then ( response => response . json ( ) )
. then ( data => {
this . setState ( {
people : data
} )
} ) ;
}
createPerson = ( event ) => {
event . preventDefault ( ) ;
fetch (
'/people' ,
{
method : 'POST' ,
headers : {
'Accept' : 'application/json; charset=utf-8' ,
'Content-Type' : 'application/json;charset=UTF-8'
} ,
body : JSON . stringify ( {
Name : this . state . newPersonName ,
Age : this . state . newPersonAge
} )
}
)
. then ( response => response . json ( ) )
. then (
( data ) => {
this . setState ( {
people : data
} )
}
)
}
deletePerson = ( event ) => {
fetch (
'/people/' + event . target . value ,
{
method : 'DELETE'
}
)
. then ( response => response . json ( ) )
. then (
( data ) => {
this . setState ( {
people : data
} )
}
)
}
changeNewPersonAge = ( event ) => {
this . setState ( {
newPersonAge : event . target . value
} ) ;
}
changeNewPersonName = ( event ) => {
this . setState ( {
newPersonName : event . target . value
} ) ;
}
render = ( ) => {
return < div >
< h2 > Create Person < / h 2 >
< form onSubmit = { this . createPerson } >
< input onKeyUp = { this . changeNewPersonName } type = "text" placeholder = "name" / > < br / >
< input onKeyUp = { this . changeNewPersonAge } type = "number" placeholder = "age" / > < br / >
< input type = "submit" value = "Create Person" / >
< / f o r m >
< h2 > List of People < / h 2 >
< ul >
{
this . state . people . map (
( person , index ) => {
return < li key = { index } >
{ person . name } : { person . age }
< button value = { person . personId } onClick = { this . deletePerson } > DELETE < / b u t t o n >
render ( ) {
return (
< div >
< h1 > Hello , world ! < / h 1 >
< p > Welcome to your new single - page application , built with : < / p >
< ul >
< li > < a href = 'https://get.asp.net/' > ASP . NET Core < /a> and <a href='https:/ / msdn . microsoft . com / en - us / library / 67 ef8sbd . aspx ' > C # < / a > f o r c r o s s - p l a t f o r m s e r v e r - s i d e c o d e < / l i >
< li > < a href = 'https://facebook.github.io/react/' > React < / a > f o r c l i e n t - s i d e c o d e < / l i >
< li > < a href = 'http://getbootstrap.com/' > Bootstrap < / a > f o r l a y o u t a n d s t y l i n g < / l i >
< / u l >
< p > To help you get started , we have also set up : < / p >
< ul >
< li > < strong > Client - side navigation < / s t r o n g > . F o r e x a m p l e , c l i c k < e m > C o u n t e r < / e m > t h e n < e m > B a c k < / e m > t o r e t u r n h e r e . < / l i >
< li > < strong > Development server integration < / s t r o n g > . I n d e v e l o p m e n t m o d e , t h e d e v e l o p m e n t s e r v e r f r o m < c o d e > c r e a t e - r e a c t - a p p < / c o d e > r u n s i n t h e b a c k g r o u n d a u t o m a t i c a l l y , s o y o u r c l i e n t - s i d e r e s o u r c e s a r e d y n a m i c a l l y b u i l t o n d e m a n d a n d t h e p a g e r e f r e s h e s w h e n y o u m o d i f y a n y f i l e . < / l i >
< li > < strong > Efficient production builds < / s t r o n g > . I n p r o d u c t i o n m o d e , d e v e l o p m e n t - t i m e f e a t u r e s a r e d i s a b l e d , a n d y o u r < c o d e > d o t n e t p u b l i s h < / c o d e > c o n f i g u r a t i o n p r o d u c e s m i n i f i e d , e f f i c i e n t l y b u n d l e d J a v a S c r i p t f i l e s . < / l i >
< / u l >
< p > The < code > ClientApp < / c o d e > s u b d i r e c t o r y i s a s t a n d a r d R e a c t a p p l i c a t i o n b a s e d o n t h e < c o d e > c r e a t e - r e a c t - a p p < / c o d e > t e m p l a t e . I f y o u o p e n a c o m m a n d p r o m p t i n t h a t d i r e c t o r y , y o u c a n r u n < c o d e > n p m < / c o d e > c o m m a n d s s u c h a s < c o d e > n p m t e s t < / c o d e > o r < c o d e > n p m i n s t a l l < / c o d e > . < / p >
< form id = { person . personId } onSubmit = { this . updatePerson } >
< input onKeyUp = { this . changeUpdatePersonName } type = "text" placeholder = "name" / > < br / >
< input onKeyUp = { this . changeUpdatePersonAge } type = "number" placeholder = "age" / > < br / >
< input type = "submit" value = "Update Person" / >
< / f o r m >
< / l i >
}
)
}
< / u l >
< / d i v >
) ;
}
}