這是來自非常簡單的blog react-redux應用程序的reducer_posts.js
。在這個reducer中返回{... state,}究竟意味着什麼?
import _ from 'lodash';
import { FETCH_POSTS, FETCH_ONE_POST, DELETE_POST } from '../actions/index';
export default function (state = {}, action) {
switch (action.type) {
case DELETE_POST:
return _.omit(state, action.payload);
case FETCH_ONE_POST:
return { ...state, [action.payload.data._id]: action.payload.data };
case FETCH_POSTS:
return _.mapKeys(action.payload.data, '_id');
default:
return state;
}
}
_.omit(state, action.payload)
而不action.payload返回狀態,它是在沒有刪除後恢復狀態。
_.mapKeys(action.payload.data, '_id')
創建具有初始對象相同值的對象,但新的對象已經從action.payload.data._id
邁出了新的關鍵,但我不能只得到什麼代碼,這片語法的確切含義:
return { ...state, [action.payload.data._id]: action.payload.data };
這行代碼是做什麼的?這是什麼意思?
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator](https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator) – Boky
[反應中的三個點有什麼作用?](https://stackoverflow.com/questions/ 31048953/what-does-three-dots-in-react-do) –
嘿,我的回答(https://stackoverflow.com/a/44245917/2545680)有幫助嗎?什麼都不清楚? –