Redux使用实例

//index.js
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import App from './components/app'
import {createStore} from 'redux'
import {counter} from './redux/reducers'
// 生成一个store对象
const store = createStore(counter); // 内部会第一次调用reducer函数得到state
console.log(store,store.getState());
function render(){
    ReactDOM.render(<App store={store}/>,document.getElementById('root'));
}
// 初始化渲染
render();
// 订阅监听。store中的状态变化了,就自动重绘。
store.subscribe(render);
// /redux/reducer.js
// 包含n个reducer函数的模块 整个文件作为对象
import {INCREMENT,DECREMENT} from './action-type'
export function counter(state = 0,action){
    console.log('counter()',state,action)
    switch(action.type){
        case 'INCREMENT' :
            return state + action.data;
        case DECREMENT:
            return state - action.data;
        default:
            return state;
    }
}
export function xxx(){
}```

```javascript
// /components/app.jsx
import React, {Component} from 'react'
import {INCREMENT,DECREMENT} from '../redux/action-type'
export default class App extends Component{
    state = {
        count: 0
    }
    increment = () =>{
        //1.得到选择增加数量
        const number = this.select.value*1;
        //2. 调用store的方法更新状态
        this.props.store.dispatch({type: 'INCREMENT',data:number});
    }
    decrement = () =>{
        //1.得到选择增加数量
        const number = this.select.value*1;
        this.props.store.dispatch({type: DECREMENT,data:number});
    }
    incrementIfOdd = () =>{
        //1.得到选择增加数量
        const number = this.select.value*1;
        //2. 得到原本的count状态,并计算新的count
        const count = this.props.store.getState();
        // 判断,满足条件才更新
        if(count %2 ===1){
            //3. 更新状态
            this.props.store.dispatch({type: 'INCREMENT',data:number});
        }
    }
    incrementIfAsync = () =>{
        //1.得到选择增加数量
        const number = this.select.value*1;
        setTimeout(()=>{
            //3. 更新状态
            this.props.store.dispatch({type: 'INCREMENT',data:number});
        },1000)
    }
    render(){
        const count = this.props.store.getState();
        //debugger
        return (
            <div>
                <p>click {count} times</p>
                <div>
                    <select ref={select => this.select = select}>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>&nbsp;
                    <button onClick={this.increment}>+</button>
                    <button onClick={this.decrement}>-</button>
                    <button onClick={this.incrementIfOdd}>increment if odd</button>
                    <button onClick={this.incrementIfAsync}>increment if async</button>
                </div>
            </div>
        )
    }
} ```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值