There is application which uses react, redux, react-redux, redux-thunk.
react: "16.0.0-alpha.6"
redux: "3.6.0"
react-redux: "5.0.2"
redux-thunk: "2.1.0"
Concept:
Reducers:
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
});
Store:
const createStoreWithMiddleware = applyMiddleware(
thunkMiddleware
)(createStore);
const store = createStoreWithMiddleware(rootReducer);
Actions creators:
export function addMessage(message) {
return {
type: MESSAGES_ADD_MESSAGE,
message
};
}
export function addContact(contact) {
return {
type: CONTACTS_ADD_CONTACT,
contact
};
}
Why time of dispatch(addContact('Viktor +123456789')) grows depending on count of messages in store?
As I understand in time of new store construct, the messages reducer returns state reference without creating new copy of this part of store.
I have more complicated real case but concept of issue is similar.