Skip to content

Commit 771b953

Browse files
committed
优化代码结构
1 parent b4c4143 commit 771b953

File tree

6 files changed

+45
-88
lines changed

6 files changed

+45
-88
lines changed

src/page/Counter/UI/index.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/page/Counter/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { connect } from 'react-redux';
2-
import Counter from './UI/index';
2+
import React from 'react';
33

4-
const mapStateToProps = (state, ownProps) => {
5-
// console.log('mapStateToProps', state.count,ownProps);
4+
const mapStateToProps = (state) => {
65
return {
76
value: state.count
87
}
@@ -21,7 +20,14 @@ const mapDispatchToProps = {
2120
},
2221
}
2322

24-
// 容器组件
25-
// const VisibleCounter = connect(mapStateToProps, mapDispatchToProps)(Counter)
23+
function Page({ value, onIncreaseClick, onDecreaseClick }) {
24+
return (
25+
<div>
26+
<h1>{value}</h1>
27+
<button onClick={onIncreaseClick}>+</button>
28+
<button onClick={onDecreaseClick}>-</button>
29+
</div>
30+
)
31+
}
2632

27-
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
33+
export default connect(mapStateToProps, mapDispatchToProps)(Page);

src/page/CssTest/index.js

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,23 @@ import React from 'react';
22
import styled from 'styled-components';
33
import style from './index.module.css'
44

5-
// import logo from './logo.svg';s
6-
75
const StyledDiv = styled.div`
86
width: 300px;
97
height: 300px;
108
text-align: center;
11-
.red_color {
9+
.blue_color {
1210
color: red;
1311
}
1412
`;
1513

16-
function FancyBorder(props) {
17-
console.log(props);
14+
function Page() {
1815
return (
1916
<StyledDiv>
20-
{/* styled-components作用的 */}
21-
<h1 className="red_color">test</h1>
22-
{/* App.module.css里定义的全局样式 .title 和 局部样式 .red_color */}
23-
<h2 className="title">{props.value}</h2>
24-
<button className={style.red_color} onClick={props.onClick}>加1</button>
17+
<h1 className="blue_color">styled-components的样式</h1>
18+
<h2 className={style.pink_color}>App.module.css里定义的样式</h2>
19+
<h3 className="title">App.module.css里定义的全局样式</h3>
2520
</StyledDiv>
2621
);
2722
}
2823

29-
class page extends React.Component {
30-
constructor(props) {
31-
super(props);
32-
this.state = {
33-
value: 0,
34-
};
35-
this.textInput = React.createRef();
36-
this.handleClick = this.handleClick.bind(this);
37-
this.focusTextInput = this.focusTextInput.bind(this);
38-
}
39-
componentDidMount() {
40-
this.focusTextInput();
41-
}
42-
43-
focusTextInput() {
44-
// 直接使用原生 API 使 text 输入框获得焦点
45-
// 注意:通过 "current" 取得 DOM 节点
46-
this.textInput.current.focus();
47-
}
48-
handleClick() {
49-
this.setState((prevState) => {
50-
console.log('当前的count:', prevState.value);
51-
return {value: prevState.value + 1}
52-
});
53-
}
54-
render() {
55-
return (
56-
<div>
57-
<FancyBorder value={this.state.value} onClick={this.handleClick}/>
58-
<input
59-
type="text"
60-
ref={this.textInput} />
61-
</div>
62-
)
63-
}
64-
}
65-
66-
export default page;
24+
export default Page;

src/page/CssTest/index.module.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.red_color {
2-
color: red;
1+
.pink_color {
2+
color: pink;
33
}
44

55
:global(.title) {

src/page/Single/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from "react";
2+
import { connect } from 'react-redux';
3+
4+
function mapStateToProps(state) {
5+
return {
6+
num: state.count,
7+
};
8+
}
9+
10+
function Page({ num }) {
11+
return (<h1>{num}</h1>);
12+
}
13+
14+
export default connect(mapStateToProps)(Page);

src/router/index.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
import React, { useState, useEffect } from "react";
1+
import React from "react";
22
import { HashRouter ,BrowserRouter, Route, Link,Switch } from "react-router-dom";
33
import Home from '../page/CssTest/index'
44
import Counter from '../page/Counter/index'
5-
import { connect } from 'react-redux';
5+
import Single from '../page/Single/index'
66

77

8-
function mapStateToProps(state) {
9-
return {
10-
num: state.count,
11-
};
12-
}
13-
function App({num}) {
14-
const [count, setCount] = useState(0);
15-
console.log('222222', num);
16-
useEffect(() => {
17-
console.log('111111111' , num);
18-
}, [num]);
8+
9+
function App() {
10+
// const [count, setCount] = useState(0);
11+
1912
return (
2013
<HashRouter>
2114
<div>
2215
<Header />
23-
<div>
24-
test---
25-
{num}
26-
</div>
2716
<div>
2817
<Route exact path="/" component={Home} />
2918
<Route path="/about" component={About} />
3019
<Route path="/hasChildren" component={HasChildren} />
3120
<Route path="/counter" component={Counter} />
21+
<Route path="/single" component={Single} />
3222
</div>
3323

3424
</div>
@@ -89,8 +79,11 @@ function Header() {
8979
<li>
9080
<Link to="/counter">Counter</Link>
9181
</li>
82+
<li>
83+
<Link to="/single">single</Link>
84+
</li>
9285
</ul>
9386
);
9487
}
9588

96-
export default connect(mapStateToProps)(App);
89+
export default App;

0 commit comments

Comments
 (0)