第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

redux從后端獲得數(shù)據(jù)怎么傳遞給state

redux從后端獲得數(shù)據(jù)怎么傳遞給state

冉冉說 2018-12-19 18:19:52
比如一個(gè)博客,首次進(jìn)入頁面,我從后端同時(shí)獲得了user,article,comment,我是一個(gè)一個(gè)傳遞給他們嗎?就像這樣const article = (article) => ({type: 'INIT_ARTICLE', article});const user = (user) => ({type: 'INIT_USER', user});const comment = (user) => ({type: 'INIT_COMMENT', comment});獲取后觸發(fā)三個(gè)dispatch嗎?目前我的做法const blogReducer = (state = {}, action) => {    switch (action.type) {        case RECEIVE_DATA:            return action.data;        default:            return {                comment: commentHelper(state.comment, action),                article: articleHelper(state.article, action),                user: userHelper(state.user, action),            }    }}    const commentHelper = (state = [], action) => {        switch (action.type) {            case 'ADD_COMMENT':                return [...state, action.comment];            default:                return state;         }        }獲取數(shù)據(jù)時(shí)觸發(fā)RECEIVE_DATA,更改comment時(shí),觸發(fā)ADD_COMMENT就行。不知是否合理。
查看完整描述

1 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

更好的方式是合并成一個(gè)dispatch:


const data = (user, article, comment) => ({type: 'INIT_BLOG', data: {user, article, comment}})

dispatch 這個(gè)Action


reducer:


const blogReducer = (state = {}, action) => {

    switch (action.type) {

        case 'INIT_BLOG':

            return {...state,

                comment: action.data.comment,

                article: action.data.article,

                user: action.data.user

   

            };

        case 'ADD_COMMENT':

            return {...state, comment: [...state, action.data.comment]};

              

        default:

            state;

    }

}

你上面的寫法功能是可以的,但不符合reducer的習(xí)慣寫法


查看完整回答
反對 回復(fù) 2019-01-10
  • 1 回答
  • 0 關(guān)注
  • 591 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號