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

I want to get the current action from store. #1243

Closed
whatifif opened this issue Jan 15, 2016 · 3 comments
Closed

I want to get the current action from store. #1243

whatifif opened this issue Jan 15, 2016 · 3 comments

Comments

@whatifif
Copy link

store has "subscribe" method to notify "the change of state" to all the subscribing clients.

It will be convenient if the current action is able to be sent to the clients in 'subscribe' method.

Current state can be obtained from store.getState().
But there is no such method to get the current action from store
( for example, store.getAction() which will return the current action dispatched to the store )

Any idea is appreciated.

@gaearon
Copy link
Contributor

gaearon commented Jan 15, 2016

Please see #1057, this is the same request.

@gaearon gaearon closed this as completed Jan 15, 2016
@gaearon
Copy link
Contributor

gaearon commented Jan 15, 2016

Also #1091, #622.

@whatifif
Copy link
Author

I found an exact solution to this issue. So I leave the link below.
This has an advantage: Action can be controlled before it is dispatched to the store.

Sending Actions To The Server Using Redux Middleware

[ http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html#sending-actions-to-the-server-using-redux-middleware ]


src/components/index.jsx

import {createStore, applyMiddleware} from 'redux';
import io from 'socket.io-client';
import remoteActionMiddleware from './remote_action_middleware';

const socket = io(`${location.protocol}//${location.hostname}:8090`);
socket.on('state', state =>
  store.dispatch(setState(state))
);

const createStoreWithMiddleware = applyMiddleware(
  remoteActionMiddleware(socket)
)(createStore);
const store = createStoreWithMiddleware(reducer);

src/remote_action_middleware.js

export default socket => store => next => action => {
  if (action.meta && action.meta.remote) {
    socket.emit('action', action);
  }
  return next(action);
}

src/action_creators.js

export function vote(entry) {
  return {
    meta: {remote: true},
    type: 'VOTE',
    entry
  };
}

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

2 participants