File tree 3 files changed +44
-13
lines changed 3 files changed +44
-13
lines changed Original file line number Diff line number Diff line change 1
1
import 'bootstrap/dist/css/bootstrap.min.css' ;
2
- import React from 'react' ;
3
- import logo from './logo.svg' ;
4
- import './App.css' ;
2
+ import { useState } from 'react' ;
3
+ import { EditTitle } from './EditTitle' ;
5
4
import { PageTitle } from './PageTitle' ;
6
- import { StateDemo } from './StateDemo' ;
7
5
8
6
function App ( ) {
9
7
8
+ const [ pageTitle , setPageTitle ] = useState < string > ( "Example Page" ) ;
10
9
11
10
return (
12
11
< div >
13
- < PageTitle title = "My Page" />
14
- { /* <PageTitle title="My Other Page" /> */ }
12
+ < PageTitle title = { pageTitle } />
15
13
< hr />
16
- < StateDemo initalValue = { 10 } multipleOfTen = { ( val ) => { alert ( val ) ; } } />
14
+ < EditTitle onChange = { ( name ) => setPageTitle ( name ) } />
15
+ { /* <StateDemo initalValue={10} multipleOfTen={(val) => { alert(val); }} />
17
16
<hr />
18
- < StateDemo initalValue = { - 100 } />
17
+ <StateDemo initalValue={-100} /> */ }
19
18
</ div >
20
19
) ;
21
20
}
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+
3
+ type Props = {
4
+ onChange : ( newTitle : string ) => void ;
5
+ } ;
6
+
7
+ type State = {
8
+ value : string ;
9
+ } ;
10
+
11
+ export class EditTitle extends React . Component < Props , State > {
12
+
13
+ constructor ( props : Props ) {
14
+ super ( props ) ;
15
+ this . state = {
16
+ value : "" ,
17
+ } ;
18
+ }
19
+
20
+ changeTitle ( ) {
21
+ this . props . onChange ( this . state . value ) ;
22
+ }
23
+
24
+
25
+ render ( ) {
26
+ return (
27
+ < >
28
+ < form >
29
+ < input type = "text" value = { this . state . value }
30
+ onChange = { ( e ) => this . setState ( { value : e . target . value } ) } />
31
+ < button onClick = { ( ) => this . changeTitle ( ) } type = "button" > Change</ button >
32
+ </ form >
33
+ </ >
34
+ ) ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -6,13 +6,9 @@ type Props = {
6
6
7
7
export class PageTitle extends React . Component < Props > {
8
8
9
- constructor ( props : Props ) {
10
- super ( props ) ;
11
- }
12
-
13
9
render ( ) {
14
10
return (
15
- < h1 style = { { color : 'red' } } > { this . props . title } </ h1 >
11
+ < h1 > { this . props . title } </ h1 >
16
12
) ;
17
13
}
18
14
}
You can’t perform that action at this time.
0 commit comments