incrementTopicRating decrementTopicRating.
:
export default (state = [], action) => {
switch (action.type) {
case 'ADD_TOPIC': return addTopic(state, action);
case 'INCREMENT_TOPIC_RATING': return incrementTopicRating(state, action);
case 'DECREMENT_TOPIC_RATING': return decrementTopicRating(state, action);
default: return state;
}
};
const addTopic = (state, action) => {
let { title, body, id, rating } = action;
let newTopic = { title, body, id, rating };
return [newTopic, ...state];
};
const incrementTopicRating = (state, action) => {
const newState = state.map((topic) => {
if (+topic.id === +action.id) {
return Object.assign({}, topic, { rating: topic.rating+1 });
}
return topic;
});
return newState;
};
const decrementTopicRating = (state, action) => {
return state.map((topic) => {
if (+topic.id === +action.id) {
return Object.assign({}, topic, { rating: topic.rating-1 });
}
return topic;
});
};
JSBIN : https://jsbin.com/nihabaqumo
, , , facebook Immutable . , .
:
map array topic, , ? , incrementTopicRating . ?
map array, object.
react-redux , , SingleTopic ( ), . , , true , . SingleTopic .
: http://rackt.org/redux/docs/Troubleshooting.html
Connect, shouldComponentUpdate, , / : < 3 >