Skip to content

@types/redux: Unable to get ActionCreator from ActionCreatorsMapObject #2064

Closed
@RickyInAdi

Description

@RickyInAdi

version info
"@types/redux": "^3.6.31"

Issue: #
Unable to get ActionCreator from ActionCreatorsMapObject.

ActionCreatorsMapObject works perfectly for binding all ActionCreator(s) e.g., see homepage-container.ts

But if i want to bind single ActionCreator, I have to "export" all actionCreator(s) functions, and then import * as actionCreators to be able to bind single ActionCreator to dispatch. (see useredit-container.ts below).

potential solution
Wouldn't it be nice to use the map in both cases, and then call map.getActionCreator(string) in bindActionCreators

users-actioncreators.ts

export const fetchUsers: ActionCreator<Action> = (): Action => {
//returns action 
}

export const fetchUsersFromApiWithThunk: ThunkAction<{}, State, {}> = (): Function => {
//returns a function
}

export const addUser: ActionCreator<Action> = (user: User): Action => {
//returns action 
}

const UsersActionCreatorsMap: ActionCreatorsMapObject = {
    'fetchUsers': fetchUsersFromApiWithThunk,
    'addUser': addUser
}

export default UsersActionCreatorsMap;

homepage-container.ts

import UsersActionCreatorsMap from '../../ui-state/action-creators/users-actioncreators';

function mapDispatchToProps(dispatch: Dispatch<State>) {
    console.log(`HomepageContainer: mapping actions: UsersActionCreator`);
    return {
        actions: bindActionCreators(UsersActionCreatorsMap, dispatch)
    }
};
const HomePageContainer = connect(mapStateToProps, mapDispatchToProps)(Homepage);
export default HomePageContainer;

useredit-container.ts

import * as usersActionCreators from '../../ui-state/action-creators/users-actioncreators';

function mapDispatchToProps(dispatch: Dispatch<State>) {
    return {
        actions: bindActionCreators(usersActionCreators.addUser, dispatch)
    }
};
const UserEditContainer = connect(mapStateToProps, mapDispatchToProps)(UserEdit);
export default UserEditContainer;

Thanks for your time.
Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions