Maneira correta de atualizar o estado em redutores redux
Eu sou um novato em redux e sintaxe es6. Eu faço meu aplicativo com o tutorial oficial de redux, e com issoexemplo.
Há um trecho JS abaixo. Meu ponto - para definir os casos REQUEST_POST_BODY e RECEIVE_POST_BODY no redutor de postagens. Principal difícil - encontrar e atualizar o objeto certo na loja.
Eu tento usar o código do exemplo:
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
})
Mas usava uma matriz simples de postagens. Não é necessário encontrar a postagem certa por ID.
Aqui meu código:
const initialState = {
items: [{id:3, title: '1984', isFetching:false}, {id:6, title: 'Mouse', isFetching:false}]
}
// Reducer for posts store
export default function posts(state = initialState, action) {
switch (action.type) {
case REQUEST_POST_BODY:
// here I need to set post.isFetching => true
case RECEIVE_POST_BODY:
// here I need to set post.isFetching => false and post.body => action.body
default:
return state;
}
}
function requestPostBody(id) {
return {
type: REQUEST_POST_BODY,
id
};
}
function receivePostBody(id, body_from_server) {
return {
type: RECEIVE_POST_BODY,
id,
body: body_from_server
};
}
dispatch(requestPostBody(3));
dispatch(receivePostBody(3, {id:3, body: 'blablabla'}));