search

Home  >  Q&A  >  body text

javascript - The data requested by the action is given to the reducer. Why is the data not mapped to the store after the reducer is executed?

// 这是reducer
export const requestUserInfo = (state = {}, action) => {
    switch(action.type) {
        case 'HEADER_RECEIVE_USERINFO':
            console.log(action.data)// 有值
            return Object.assign({}, state, action.data)
            break
        default:
            return state
    }
}
// 这是action
function receiveUserInfo (data) {
    return {
        type: 'HEADER_RECEIVE_USERINFO',
        data
    }
}


export function requestUserInfo () {
    return dispatch => {
        return axios.post('/user/getScores', qs.stringify({
                token: token,
                uid: uid
            }))
            .then(res => {
                if(res.data.status !== 200) {
                    message.error(res.data.message)
                    return state
                }

                return { ...res.data.attachment}
            })
            .then(data => {
                dispatch(receiveUserInfo(data))
            })
    }
}

Both action and reducer can be executed normally. I monitored the store in the router, but the store has never changed. Did I write something wrong somewhere?
Someone help me, I've been stuck on this problem all day, I don't know where the problem is

黄舟黄舟2819 days ago670

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-06-12 09:23:45

    Object.assign is a shallow copy and will only process the first layer
    state = {

    a: name,
    b: {
        child: {}    // 无论child怎么改变都不会触发state的change, 因为b的引用没有变
    }

    }

    Is the return value of your action.data an object?

    reply
    0
  • Cancelreply