Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RickyInAdi opened this issue Oct 29, 2016 · 1 comment
Closed

Comments

@RickyInAdi
Copy link

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

@RickyInAdi
Copy link
Author

Sorry, solution was simple.
UsersActionCreatorsMap['addUser']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant