react中使用react-redux实现简单的计数器

创建react项目

# 全局安装脚手架
npm i create-react-app -g
# 构建一个react的项目
create-react-app react-redux-demo

安装赖

cnpm i redux -S
cnpm i react-redux -S

创建目录文件

在这里插入图片描述

编写action

// actions/countAction.js

export const Add = {
    type:'add'
}
export const Sub  = {
    type:'sub'
}

编写reducers

//	reducers /counter.js

export default function counterReducer(state = initialState, action) {
    switch (action.type) {
        case 'add': return Object.assign({}, state, {
            count: state.count + 1
        });
        case 'sub': return Object.assign({}, state, {
            count: state.count - 1
        });
        default: return state;
    }
}
//设置一个初始值
const initialState = {
    count: 0,
}
//	reducers/index.js

import { combineReducers } from 'redux'
import counter from './counter'

const reducers = combineReducers({
    counter
})

export default reducers

编写容器组件

//	containers/app.js

import React from 'react';
import { connect } from 'react-redux'
import counter from '../pages/counter'
import {Add,Sub} from "../redux/actions/countAction";

const mapStateToProps = state => {
    return {
        count: state.counter.count,
    }
}

const mapDispatchToProps = dispatch => {
    return {
        countAdd: (count) => {
            dispatch(Add)
        },
        countSub: (count)=> {
            dispatch(Sub)
        }
    }
}

const App = connect(
    mapStateToProps,
    mapDispatchToProps
)(counter)

export default App;

编写展示组件

//	pages/counter.js

import React from 'react'

class Counter extends React.Component {
    constructor(props) {
        super(props);
        this.countAdd = this.countAdd.bind(this)
        this.countSub = this.countSub.bind(this)
    }
    countAdd(){
        this.props.countAdd();
    }
    countSub(){
        this.props.countSub();
    }
    render() {
        return(
            <div style={{
                textAlign:'center'
            }}>
                <button onClick={this.countSub}>减一</button>
                <span style={{
                    display:'inline-block',
                    width:'50px',
                    textAlign:'center'
                }}>
                    {this.props.count}
                </span>
                <button onClick={this.countAdd}>加一</button>
            </div>
        )
    }
}
export default Counter

编写入口文件

//	src/inde.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from "./containers/app";
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import reducers from './redux/reducers'

const store = createStore(reducers)
ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>
    , document.getElementById('root')
);

运行项目

npm start

在这里插入图片描述
GitHub地址: react-redux-demo.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值