44_redux_comment应用_redux版本_同步功能

项目结构:

components里面的东西没变,将app.jsx移动至containers中

/*
* 包含所有action的type名称常量
* */
//添加评论
export const ADD_COMMENT = 'add_comment';
//删除评论
export const DELETE_COMMENT = 'delete_comment';
action-types.js
/*
* 包含了所有的action creator(action的工厂函数)
* */
import {ADD_COMMENT, DELETE_COMMENT} from './action-types'
// 同步添加
export const addComment = (comment) => (
    {type: ADD_COMMENT, data: comment}
)
// 同步删除
export const deleteComment = (index) => (
    {type: DELETE_COMMENT, data: index}
)
actions.js
/*
* 包含n个reducer函数(根据老的state和action返回一个新的state)
* */

import {ADD_COMMENT, DELETE_COMMENT} from './action-types'

const initComments = [
    {username: 'Tom', content: 'React挺好的!'},
    {username: 'Jack', content: 'React太难了!'},
    {username: 'Jensen', content: '干就完了!'}
]

export function comments(state = initComments, action) {
    switch (action.type) {
        case ADD_COMMENT:
            return [action.data, ...state]
        case DELETE_COMMENT:
            return state.filter((comment, index) => index !== action.data)
        default:
            return state
    }
}
reducers.jsx
/*
* redux最核心的管理对象store
* */
import {createStore, applyMiddleware} from 'redux'
import {comments} from './reducers'
import thunk from 'redux-thunk'

export default createStore(
    comments,
    applyMiddleware(thunk)
)
store.js
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'

import store from './redux/store'
import App from './containers/app/app'

ReactDOM.render((
    <Provider store={store}>
        <App/>
    </Provider>
), document.getElementById('root'));
index.js
import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'

import CommentAdd from '../../components/comment-add/comment-add'
import CommentList from '../../components/comment-list/comment-list';
import {addComment, deleteComment} from '../../redux/actions'

class App extends React.Component {
    //定义数据
    static propTypes = {
        comments: PropTypes.array.isRequired,
        addComment: PropTypes.func.isRequired,
        deleteComment: PropTypes.func.isRequired
    }

    componentDidMount() {

    }


    render() {
        const {comments, addComment, deleteComment} = this.props
        return (
            <div>
                <header className="site-header jumbotron">
                    <div className="container">
                        <div className="row">
                            <div className="col-xs-12">
                                <h1>请发表对React的评论</h1>
                            </div>
                        </div>
                    </div>
                </header>
                <div className="container">
                    <CommentAdd addComment={addComment}/>
                    <CommentList comments={comments} deleteComment={deleteComment}/>
                </div>
            </div>

        )
    }
}

export default connect(
    state => ({comments: state}),// 说明state就是comments数组
    {addComment, deleteComment}
)(App)
app.jsx

 

转载于:https://www.cnblogs.com/zhanzhuang/p/10758865.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值