Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit 72062f6

Browse files
committed
全面优化组件
1 parent d023d99 commit 72062f6

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

src/App.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const history = createHistory()
1111
*/
1212
import './app.less'
1313

14-
import * as globalActions from 'actions/global'
14+
import * as global from 'actions/global'
1515
import asyncComponent from './AsyncComponent'
1616

1717
import homeContainer from 'containers/Home/HomeContainer'
@@ -20,8 +20,8 @@ const Search = asyncComponent(() => import(/* webpackChunkName: "search" */ "./c
2020
const BookList = asyncComponent(() => import(/* webpackChunkName: "bookList" */ "./containers/BookList/BookListContainer"))
2121

2222
@connect (
23-
state => state,
24-
dispatch => bindActionCreators(globalActions, dispatch)
23+
state => {return {...state.global}},
24+
dispatch => bindActionCreators(global, dispatch)
2525
)
2626
export default class App extends React.Component {
2727

@@ -32,7 +32,7 @@ export default class App extends React.Component {
3232
}
3333

3434
render() {
35-
const { animateCls } = this.props.global
35+
const { animateCls } = this.props
3636
return (
3737
<Router history={history}>
3838
<Route render={({ location }) => {

src/containers/BookList/BookListContainer.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
/**
22
* Created by Administrator on 2016/7/1.
33
*/
4-
import React from 'react';
5-
import { bindActionCreators } from 'redux';
6-
import { connect } from 'react-redux';
7-
import PropTypes from 'prop-types';
4+
import React from 'react'
5+
import { bindActionCreators } from 'redux'
6+
import { connect } from 'react-redux'
7+
import PropTypes from 'prop-types'
88

9-
/*component*/
10-
11-
@connect(
12-
state => state
13-
)
149
export default class BookListContainer extends React.Component {
1510

1611
constructor(props) {
1712
super(props);
1813
}
1914

20-
componentWillMount() {
21-
22-
}
23-
2415
render() {
2516
const { match } = this.props
2617
return(

src/containers/Home/HomeContainer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import PropTypes from 'prop-types'
99
//关于import什么时候用{},什么时候不用大括号,通过那个插件或者组件是否包含default来判断,如果包含,则不需要{}
1010

1111
/*actions*/
12-
import * as homeActions from 'actions/home'
13-
import * as globalActions from 'actions/global'
12+
import * as home from 'actions/home'
13+
import * as global from 'actions/global'
1414

1515
/*component*/
1616
import Header from 'components/Home/Header'
@@ -26,13 +26,13 @@ import 'containers/Home/styles/home.less'
2626
/**
2727
* connect中间件
2828
* connect一定要写在需要传递参数的组件头部,因为这是语法规则,只对当前关联的组件生效,和java的原理是一致的
29-
* state用法:state => state(传递全部state)
29+
* state用法:state => state(传递全部state), {return {...state.home, ...state.global}}(n个state)
3030
* dispatch用法:(单个action)bindActionCreators(navActions, dispatch),(多个action)bindActionCreators({...action1, ...action2}, dispatch)
3131
*/
3232

3333
@connect(
3434
state => {return {...state.home}},
35-
dispatch => bindActionCreators({...homeActions, ...globalActions}, dispatch)
35+
dispatch => bindActionCreators({...home, ...global}, dispatch)
3636
)
3737
export default class HomeContainer extends React.Component {
3838

src/containers/Search/SearchContainer.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ import { connect } from 'react-redux'
77
import PropTypes from 'prop-types'
88

99
/*actions*/
10-
import * as searchActions from 'actions/search'
11-
import * as globalActions from 'actions/global'
10+
import * as search from 'actions/search'
11+
import * as global from 'actions/global'
1212

1313
import Header from 'components/Search/Header'
1414
import HotSearch from 'components/Search/HotSearch'
1515

1616
import 'containers/Search/styles/search.less'
1717

1818
@connect(
19-
state => state,
20-
dispatch => bindActionCreators({...searchActions, ...globalActions}, dispatch)
19+
state => {return {...state.search}},
20+
dispatch => bindActionCreators({...search, ...global}, dispatch)
2121
)
2222
export default class SearchContainer extends React.Component {
23-
2423
constructor(props) {
2524
super(props)
2625
this.hotClick = this.hotClick.bind(this)
@@ -29,25 +28,21 @@ export default class SearchContainer extends React.Component {
2928
currentHot: ''
3029
}
3130
}
32-
3331
componentWillMount() {
3432
console.log('进入搜索页面')
3533
this.props.receiveHotSearch()
3634
}
37-
3835
upDateValue(value) {
3936
this.setState({currentHot: value})
4037
}
41-
4238
hotClick(text) {
4339
this.setState({currentHot: text})
4440
}
45-
4641
render() {
47-
const { hotData } = this.props.search
42+
const { hotData } = this.props
4843
const { currentHot } = this.state
4944
return (
50-
<div key={this.props} style={{height: '100vh'}}>
45+
<div style={{height: '100vh'}}>
5146
<Header handleClick={this.props.currentAnimate}
5247
currentHot={currentHot}
5348
upDateValue={this.upDateValue}
@@ -78,7 +73,6 @@ export default class SearchContainer extends React.Component {
7873
)
7974
}
8075
}
81-
8276
SearchContainer.propTypes = {
8377
hotData: PropTypes.array
8478
}

0 commit comments

Comments
 (0)