Commit 258b90c3 by 王树冬

更新获取状态的写法

parent 9291f0c9
## 2018年11月26日
优化action 代码
...@@ -8,7 +8,7 @@ import configStore from './store' ...@@ -8,7 +8,7 @@ import configStore from './store'
import './app.styl' import './app.styl'
const store = configStore() const store = configStore
class App extends Component { class App extends Component {
......
...@@ -2,7 +2,8 @@ import Taro, { Component } from '@tarojs/taro' ...@@ -2,7 +2,8 @@ import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components' import { View, Button, Text } from '@tarojs/components'
import { connect } from '@tarojs/redux' import { connect } from '@tarojs/redux'
import { add, minus, asyncAdd, list } from '../../actions/counter' // import { add, minus, asyncAdd, list } from '../../actions/counter'
import counterAction from '../../actions/counter'
import './index.styl' import './index.styl'
...@@ -23,16 +24,18 @@ import './index.styl' ...@@ -23,16 +24,18 @@ import './index.styl'
// // dispatch(list()) // // dispatch(list())
// // } // // }
// })) // }))
@connect(
state => ({
list: state.counter.list, // @connect(
}), // state => ({
(dispatch) => ({ // list: state.counter.list,
getList(params) { // }),
dispatch(list(params)) // (dispatch) => ({
} // getList(params) {
}) // dispatch(list(params))
) // }
// })
// )
class Index extends Component { class Index extends Component {
config = { config = {
...@@ -40,7 +43,6 @@ class Index extends Component { ...@@ -40,7 +43,6 @@ class Index extends Component {
} }
constructor(props) { constructor(props) {
super(props) super(props)
console.log('props', props);
// this.state = { date: new Date() } // this.state = { date: new Date() }
// let params = { // let params = {
// page: 2, per_page: 2 // page: 2, per_page: 2
...@@ -49,14 +51,14 @@ class Index extends Component { ...@@ -49,14 +51,14 @@ class Index extends Component {
page: 1, per_page: 10 page: 1, per_page: 10
} }
if (Object.keys(props).length === 0) return if (Object.keys(props).length === 0) return
props.getList(this.state) counterAction.list(this.state)
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
console.log(this.props, nextProps) console.log(this.props, nextProps)
} }
componentWillMount() { componentWillMount() {
console.log('thisprops', this.props); // this.props.getList(this.state)
this.props.getList(this.state) counterAction.list(this.state)
} }
componentWillUnmount() { componentWillUnmount() {
...@@ -68,13 +70,13 @@ class Index extends Component { ...@@ -68,13 +70,13 @@ class Index extends Component {
onShareAppMessage() { onShareAppMessage() {
return { return {
title: '蜂窝煤', title: 'taro issue',
path: '/pages/index/index', path: '/pages/index/index',
imageUrl: '../../common/image/share.png' imageUrl: '../../common/image/share.png'
} }
} }
onReachBottom() { onReachBottom() {
console.log('触发到底部了') // 触发到底部
this.setState({ this.setState({
page: this.state.page + 1 page: this.state.page + 1
}) })
...@@ -82,16 +84,16 @@ class Index extends Component { ...@@ -82,16 +84,16 @@ class Index extends Component {
let params = { let params = {
page: this.state.page + 1, per_page: per_page page: this.state.page + 1, per_page: per_page
} }
console.log(this.props, 'onReachBottom') counterAction.list(params)
this.props.getList(params)
} }
render() { render() {
if(!this.props.getList) return
return ( return (
<View className='index'> <View className='index'>
<View className='data'>数据列表</View> <View className='data'>数据列表</View>
<View className='news'> <View className='news'>
{ {
this.props.list.map((item, index) => { this.props.getList.map((item, index) => {
return <View className='item' key={index}> return <View className='item' key={index}>
<View className='info'> <View className='info'>
<View className='title'>{item.title}</View> <View className='title'>{item.title}</View>
...@@ -109,5 +111,9 @@ class Index extends Component { ...@@ -109,5 +111,9 @@ class Index extends Component {
) )
} }
} }
const mapStateToProps = (state, ownProps) => {
export default Index return {
getList: state.counter.list
}
}
export default connect(mapStateToProps)(Index)
...@@ -8,7 +8,4 @@ const middlewares = [ ...@@ -8,7 +8,4 @@ const middlewares = [
createLogger() createLogger()
] ]
export default function configStore () { export default createStore(rootReducer, applyMiddleware(...middlewares))
const store = createStore(rootReducer, applyMiddleware(...middlewares))
return store
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment