Skip to content

Commit a7ca15d

Browse files
committed
Chained components
1 parent e1a436a commit a7ca15d

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

src/App.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
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';
54
import { PageTitle } from './PageTitle';
6-
import { StateDemo } from './StateDemo';
75

86
function App() {
97

8+
const [pageTitle, setPageTitle] = useState<string>("Example Page");
109

1110
return (
1211
<div>
13-
<PageTitle title="My Page" />
14-
{/* <PageTitle title="My Other Page" /> */}
12+
<PageTitle title={pageTitle} />
1513
<hr />
16-
<StateDemo initalValue={10} multipleOfTen={(val) => { alert(val); }} />
14+
<EditTitle onChange={(name) => setPageTitle(name)} />
15+
{/* <StateDemo initalValue={10} multipleOfTen={(val) => { alert(val); }} />
1716
<hr />
18-
<StateDemo initalValue={-100} />
17+
<StateDemo initalValue={-100} /> */}
1918
</div>
2019
);
2120
}

src/EditTitle.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

src/PageTitle.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ type Props = {
66

77
export class PageTitle extends React.Component<Props> {
88

9-
constructor(props: Props) {
10-
super(props);
11-
}
12-
139
render() {
1410
return (
15-
<h1 style={{color: 'red'}}>{this.props.title}</h1>
11+
<h1>{this.props.title}</h1>
1612
);
1713
}
1814
}

0 commit comments

Comments
 (0)