Gajus
Gajus

Reputation: 73888

How to handle an event that produces changes in multiple state domains?

I am developing a map application. It consists of two components:

These components rely on the following state domains:

state.userAddress = {
    name: 'Didzioji g. 10, Vilnius'
};

state.addressSearch = {
    state: 'inactive',
    query: '',
    addresses: []
};

When user selects an address from address suggestion list it invokes an event handler onSelectAddress. onSelectAddress needs to trigger two state changes:

What is the correct way to design this?

Upvotes: 3

Views: 247

Answers (1)

Alexey Nikiforov
Alexey Nikiforov

Reputation: 377

In similar situation I choose to dispatch another action from target action.

Trying to show this in code

module.exports.locationChange = function clickElement(element) {
    return function(dispatch, getState) {
        dispatch({
            type: actionTypes.USER_CHANGE_LOCATION
        });

        dispatch({
            type: actionTypes.ADDRESS_SEARCH_HIDE
        });
   }};

I use redux-thunk for async action.

Upvotes: 3

Related Questions