react-redux状态管理

index.jsx

require('./app/lib/common.css');
import React from 'react';
import ReactDOM from 'react-dom';
import ReduxApp from './reduxApp.js'
import { store } from './redux/store.js'
import { Provider } from 'react-redux'
ReactDOM.render(
	<Provider store={store}>
		<ReduxApp />
	</Provider>,
	document.getElementById('myApp')
)
reduxApp.js
import React from 'react'
import { increment, decrement, incrementAsync } from './redux/actions.js'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
class Reduxapp extends React.Component {//在这里掉了一次坑,注意Reduxapp类名,不能用驼峰
    constructor(props) {
        super(props)
        this.increment = this.increment.bind(this)
        this.decrement = this.decrement.bind(this)
        this.incrementAsync = this.incrementAsync.bind(this)
    }
    increment() {
        const number = this.select.value * 1
        this.props.increment(number)
    }
    decrement() {
        const number = this.select.value * 1
        this.props.decrement(number)
    }
    incrementAsync() {
        const number = this.select.value * 1
        this.props.incrementAsync(number)
    }
    render() {
        const count = this.props.count//注意这里是propTypes的count而不是reducers.js中的counter
        return (
            <div>
                <h1>click {count} times</h1>
                <div>
                    <select ref={select => this.select = select}>
                        <option value='1'>1</option>
                        <option value='2'>2</option>
                        <option value='3'>3</option>
                    </select>
                    <button onClick={this.increment}>+</button>
                    <button onClick={this.decrement}>-</button>
                    <button onClick={this.incrementAsync}>async</button>
                </div>
            </div>
        )
    }

}
Reduxapp.propTypes = {
    count: PropTypes.number.isRequired,
    increment: PropTypes.func.isRequired,
    decrement: PropTypes.func.isRequired,
    incrementAsync:PropTypes.func.isRequired
}
export default connect(
    state =>({count:state}),
    {increment,decrement,incrementAsync}
)(Reduxapp)
store.js
import {createStore,applyMiddleware} from 'redux'
import thunk from 'redux-thunk'
import {counter} from './reducers.js'
export const store = createStore(counter,applyMiddleware(thunk))
reducers.js
import {INCREMENT,DECREMENT} from './action-types'
export function counter(state=0,action){
    switch(action.type){
        case INCREMENT:
            return state+action.data;
        case DECREMENT:
            return state-action.data;
        default:
            return state
    }
}
actions.js
import {INCREMENT,DECREMENT} from './action-types.js'
export const increment = (number)=>(
    {
        type:INCREMENT,data:number
    }
)
export const decrement = (number)=>(
    {
        type:DECREMENT,data:number
    }
)
export const incrementAsync = (number)=>{
    return dispatch => {
        setTimeout(()=>{
            dispatch(increment(number))
        },1000)
    }
}
action-types.js
export const INCREMENT = 'increment'
export const DECREMENT = 'decrement'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值