有應用使用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'))「期間沒有突變,爲什麼這個動作的時間會根據存儲中的郵件數量增長? –