超级详细react笔记(十三)项目篇(待办事项)redux

1 下载依赖

cnpm i redux react-redux react-thunk -S

2 编写

  • 1 创建store/index.js引入redux 引入监听 暴露函数
  • 2 reducer.js 默认参数 暴露函数
  • 3 将组件抽离要共享数据的组件 react-redux producer connect
  • 4 reducer.js 类型 进行业务操作

2.1

  • APP.js
import React,{Component} from 'react'
import {TodoHead,TodoInput,TodoList} from './component'
import {connect} from 'react-redux'
class APP extends Component{
    constructor() {
        super();
        this.state={
        }
    }
    render() {
        return(
            <div>
                <TodoHead {...this.props}/>
                <TodoInput {...this.props} myadd={this.props.addItem}/>
                <TodoList {...this.props} changeBox={this.props.changeBox} />
            </div>
        )
    }
}
const mapState = state => state
const  mapAction = dispatch=>{
    return{
        addItem(title){
            dispatch({
                type:'ADD_ITEM',
                title
            })
        },
        changeBox(id){
            dispatch({
                type:"CHECK_BOX",
                id
            })
        }
    }
}
export default connect(mapState,mapAction)(APP)
  • reducer.js
const defaultProps = {
    title:'待办事项',
    input:'请输入添加内容',
    btnText:'添加',
    columns: [
        {
            label: "id",
            prop: "id",
            width: 180
        },
        {
            label: "名称",
            prop: "title",
            width: 180
        },
        {
            label: "状态",
            prop: "status"
        }
    ],
    todos:[
        {
            id:1,
            title:"吃饭",
            status:false
        },
        {
            id:2,
            title:"睡觉",
            status:true
        },
        {
            id:3,
            title:"打豆豆",
            status:false
        }
    ]
}
export default (state=defaultProps,action)=>{
    switch (action.type) {
        case 'ADD_ITEM':
            let newAdd = JSON.parse(JSON.stringify(state))
            newAdd.todos.push({
                id:new Date().getTime(),
                title:action.title,
                status:false
            })
            return newAdd
        case 'CHECK_BOX':
            let newSub = JSON.parse(JSON.stringify(state))
           newSub.todos.map(el=>{
               if(el.id===action.id){
                   el.status=!el.status
               }
               return el
           })
            return newSub
        default:
            return state
    }
}
  • store.js
import {createStore} from 'redux'

import Reducer from './reducer'
export default createStore(Reducer)

4 拆分组件化+axios

  • actions
import {ADD_ITEM,CHECK_BOX,GET_TODO} from '../types'
import axios from '../../API'
export const addItem = (title)=>{
    return{
        type:ADD_ITEM,
        title
    }
}

export const changeBox = (id)=>{
    return{
        type:CHECK_BOX,
        id
    }
}
export const getTodosAction =()=>{
    return{
        type:GET_TODO
    }
}
export const getTodos = () => async dispatch=>{
  const res = await axios.get('/todos')
    dispatch(getTodosAction(res.data))
    console.log(res)
}
  • reducer.js
import {ADD_ITEM, CHECK_BOX, GET_TODO} from '../types'
const defaultProps = {
    title:'待办事项',
    input:'请输入添加内容',
    btnText:'添加',
    columns: [
        {
            label: "id",
            prop: "id",
            width: 180
        },
        {
            label: "名称",
            prop: "title",
            width: 180
        },
        {
            label: "状态",
            prop: "status"
        }
    ],
    todos:[
        {
            id:1,
            title:"吃饭",
            status:false
        },
        {
            id:2,
            title:"睡觉",
            status:true
        },
        {
            id:3,
            title:"打豆豆",
            status:false
        }
    ]
}
export default (state=defaultProps,action)=>{
    switch (action.type) {
        case ADD_ITEM:
            let newAdd = JSON.parse(JSON.stringify(state))
            newAdd.todos.push({
                id:new Date().getTime(),
                title:action.title,
                status:false
            })
            return newAdd
        case CHECK_BOX:
            let newSub = JSON.parse(JSON.stringify(state))
           newSub.todos.map(el=>{
               if(el.id===action.id){
                   el.status=!el.status
               }
               return el
           })
            return newSub
        case GET_TODO:
            let newData = JSON.parse(JSON.stringify(state))
            console.log(newData)
            return newData
        default:
            return state
    }
}

  • APP.js
import React,{Component} from 'react'
import {TodoHead,TodoInput,TodoList} from './component'
import {connect} from 'react-redux'
import {addItem,changeBox,getTodos} from './store/actions'
class APP extends Component{
    render() {
        return(
            <div>
                <TodoHead {...this.props}/>
                <TodoInput {...this.props} myadd={this.props.addItem}/>
                <TodoList {...this.props} changeBox={this.props.changeBox} />
            </div>
        )
    }
    componentDidMount() {
        this.props.getTodos()
    }
}
const mapState = state => state
const  mapAction = dispatch=>{
    return{
        addItem(title){
            dispatch(addItem(title))
        },
        changeBox(id){
            dispatch(changeBox(id))
        },
       getTodos(){
           dispatch(getTodos())
        }
    }
}
export default connect(mapState,mapAction)(APP)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值