使用react-redux打造一个TodoList应用

首先需要安装redux,react-redux,npm i react-redux redux --save

index.js,导入 Provider,将创建好的 store 绑定到 Provider 中

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'
import TodoList from './TodoList'
import store from './react-redux/store/index'

const App = (
    <Provider store={store}>
        <TodoList/>
    </Provider>
)

ReactDOM.render(App, document.getElementById('root'))

store/index.js,创建一个 createStore,将 reducer 引入进来

import {createStore} from 'redux'
import reducer from './reducer'

const store = createStore(reducer)

export default store

reducer.js,处理 action 中 dispatch 分发的 type

const defaultState = {
    inputValue: '',
    list: []
}

export default (state = defaultState, action) => {
    if (action.type === 'change_input_value') {
        const newState = JSON.parse(JSON.stringify(state))
        newState.inputValue = action.value
        return newState
    }
    if (action.type === 'add_item') {
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue = ''
        return newState
    }
    if (action.type === 'delete_item') {
        const newState = JSON.parse(JSON.stringify(state))
        newState.list.splice(action.index, 1)
        return newState
    }
    return state
}

TodoList.js,使用connect方法进行连接,将TodoList 这个 UI 组件打造成容器组件

import React, {Fragment} from 'react'
import {connect} from 'react-redux'

class TodoList extends React.Component {
    render() {
        const {inputValue, handleInputChange, handleClick, 
        handleDeleteItem, list} = this.props
        return (
            <Fragment>
                <div>
                    <input value={inputValue} onChange={handleInputChange}/>
                    <button onClick={handleClick}>提交</button>
                </div>
                <ul>
                    {
                        list.map((item, index) => {
                            return <li key={item} onClick={() => 
                            handleDeleteItem(index)}>{item}</li>
                        })
                    }
                </ul>
            </Fragment>
        )
    }
}

// 将 store 中的数据映射到 props 中,就可以在props中接受到 store 传递过来的数据了
const mapStateToProps = (state) => {
    return {
        inputValue: state.inputValue,
        list: state.list
    }
}

// 将 mapDispatchToProps 下的方法映射到 props 中
const mapDispatchToProps = (dispatch) => {
    return {
        handleInputChange(event) {
            const action = {
                type: 'change_input_value',
                value: event.target.value
            }
            dispatch(action)
        },
        handleClick() {
            const action = {
                type: 'add_item'
            }
            dispatch(action)
        },
        handleDeleteItem(index) {
            const action = {
                type: 'delete_item',
                index
            }
            dispatch(action)
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(TodoList)

最后可以把这个组件换成无状态组件

import React, {Fragment} from 'react'
import {connect} from 'react-redux'

const TodoList = (props) => {
    const {inputValue, handleInputChange, handleClick, 
    handleDeleteItem, list} = props
    return (
        <Fragment>
            <div>
                <input value={inputValue} onChange={handleInputChange}/>
                <button onClick={handleClick}>提交</button>
            </div>
            <ul>
                {
                    list.map((item, index) => {
                        return <li key={item} onClick={() => 
                        handleDeleteItem(index)}>{item}</li>
                    })
                }
            </ul>
        </Fragment>
    )
}

const mapStateToProps = (state) => {
    return {
        inputValue: state.inputValue,
        list: state.list
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        handleInputChange(event) {
            const action = {
                type: 'change_input_value',
                value: event.target.value
            }
            dispatch(action)
        },
        handleClick() {
            const action = {
                type: 'add_item'
            }
            dispatch(action)
        },
        handleDeleteItem(index) {
            const action = {
                type: 'delete_item',
                index
            }
            dispatch(action)
        }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(TodoList)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值