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

bindActionCreators expected a function actionCreator for key 'SET_STRING', instead received type 'string'. #2458

Closed
johhansantana opened this issue Jun 19, 2017 · 8 comments

Comments

@johhansantana
Copy link

I haven't got this error but I think since the new 3.7.0 release this is poping up.

export const SET_STRING = 'SET_STRING';
/**
 * sets the string, if none is passed, it will default to the below string.
 * @param theString
 * @return {{type: string, theString: string}}
 */
export function setString(theString: string = 'the default string') {
  return {
    type: SET_STRING,
    theString
  };
}

This seems a pretty regular redux action no? What I'm doing wrong?

@Florian-R
Copy link

See #2279

@johhansantana
Copy link
Author

@Florian-R I see what they explain there but I'm not sure how to fix it

in my container I have this

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as demoActions from '!/actions/demoActions';

...

function mapStateToProps(state) {
  return {
    demoString: state.demoString
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators(demoActions, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Home);

@elrumordelaluz
Copy link

elrumordelaluz commented Jun 19, 2017

@johhansantana Seems you have stuff exported at '!/actions/demoActions'that aren't functions (like export const SET_STRING…).

Try importing only the action creators from the file like:

import { anAction } from '!/actions/demoActions'

Here is a similar question I asked on SO.

@johhansantana
Copy link
Author

@elrumordelaluz oh I see, so instead of import * as actions just import specific actions and in the bindActionCreators do
return bindActionCreators({action1, action2, action3}, dispatch);

Right? a big app would be annoying to fix, maybe change it to a warning instead?

@anishmprasad
Copy link

Try this

import { action1,action2, … } from '../action'
// component
export default connect(mapStateToProps,{
  action1,
  action2,
  …
})(component);

@timdorr
Copy link
Member

timdorr commented Jun 19, 2017

This is a legit warning. You're attempting to pass in non-functions to bindActionCreators, which can have unexpected results. It's akin to React's change to not accept any unknown props on DOM-backed elements (e.g., <div foo="bar"> will generate a warning now).

You'll need to be more explicit about your imports and input to the function. This is a good practice regardless, as you're not expecting Redux to clean up after you. Also, most type systems (TS, Flow) should catch this for you already (prior to this version too). It's just a good practice.

@timdorr timdorr closed this as completed Jun 19, 2017
@cameracker
Copy link

cameracker commented Jun 20, 2017

I take issue with the attitude that "best practice" recommendations can just be changed willy nilly with no regard to backwards compatibility or what the tool is advertising as best practice in its own documentation. We've locked to 3.6.0 but this has just created a lot of needless work without much warning.

I do get the reasoning behind the change, but I don't think this was handled in a consumer friendly way.

@timdorr
Copy link
Member

timdorr commented Jun 26, 2017

Reverted and pushed out in 3.7.1. Thanks for the feedback everyone!

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

6 participants