有应用使用react,redux,react-redux,redux-thunk。react-reducex-thunk:调度性能问题
react: "16.0.0-alpha.6"
redux: "3.6.0"
react-redux: "5.0.2"
redux-thunk: "2.1.0"
理念:
减速:
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
export const MESSAGES_ADD_MESSAGE = 'MESSAGES_ADD_MESSAGE';
export const CONTACTS_ADD_CONTACT = 'CONTACTS_ADD_CONTACT';
export default function messages(state = { messages: [] }, action) {
switch (action.type) {
case MESSAGES_ADD_MESSAGE:
return { messages: [ ...state.messages, action.message ] };
default:
return state;
}
}
export default function contacts(state = { contacts: [] }, action) {
switch (action.type) {
case CONTACTS_ADD_CONTACT:
return { contacts: [ ...state.contacts, action.contact ] };
default:
return state;
}
}
const rootReducer = combineReducers({
contacts,
messages
});
商店:
const createStoreWithMiddleware = applyMiddleware(
thunkMiddleware
)(createStore);
const store = createStoreWithMiddleware(rootReducer);
操作的创造者:
export function addMessage(message) {
return {
type: MESSAGES_ADD_MESSAGE,
message
};
}
export function addContact(contact) {
return {
type: CONTACTS_ADD_CONTACT,
contact
};
}
为什么调度时间(addContact('Viktor +123456789'))的增长取决于存储中消息的数量?
据我所知,在新的商店构造的时候,消息reducer返回状态引用,而不创建这部分商店的新副本。
我有更复杂的实际案例,但问题的概念是类似的。
好吧,但在这种情况下,在减速器中的开关操作员的默认情况是什么? –
我们在默认情况下不会改变状态,所以返回参考是安全的。 – Akhil
好吧,如果邮件的状态在dispatch(addContact('Viktor +123456789'))“期间没有突变,为什么这个动作的时间会根据存储中的邮件数量增长? –