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

Dispatch a redux action from an external javascript module #1714

Closed
rpnna opened this issue May 11, 2016 · 2 comments
Closed

Dispatch a redux action from an external javascript module #1714

rpnna opened this issue May 11, 2016 · 2 comments
Labels

Comments

@rpnna
Copy link

rpnna commented May 11, 2016

Hi guys,
I'm working on a react/redux application, which makes use of an external module (written in plain javascript):

module.exports = {
    ...
    // Some functions here..
    someFunction () {
        dispatch(setSomething());
    }
    ...
};

I need to dispatch an action on the existing store, previously built and linked to the React application.
What's the best way?

Thank you

@gaearon
Copy link
Contributor

gaearon commented May 11, 2016

This is just JavaScript, so the answer is the same as with any JavaScript code.
If you need a module to have access to a function, pass that function to that module.

module.exports = {
  someFunction (dispatch) {
    dispatch(setSomething());
  }
};

or

module.exports = function (dispatch) {
  return {
    someFunction () {
      dispatch(setSomething());
    }
  }
};

Finally, you can (if you really want) put a store in a separate module as a singleton, but this makes testing and universal apps considerably harder.

@rpnna
Copy link
Author

rpnna commented May 11, 2016

@gaearon thanks, I also have solved this way, it was a fake problem..I really appreciate your work and this project.

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

No branches or pull requests

2 participants