-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Is it possible to get action and state in store.subscribe? #580
Comments
I made some hacking around this https://github.com/lapanoid/redux-remote/blob/master/src/createSubscribeOnStateStore.js but I am sure it is not the best way to do this. Waiting forward for @gaearon comment. |
This is not something we want to include by default. It's a hack around the paradigm, so if you really need it, please do so at your own risk. Non-React WayThe easiest way is to have a reducer that remembers just the last action: function lastAction(state = null, action) {
return action;
} Then you can use import { combineReducers, createStore } from 'redux';
const rootReducer = combineReducers({
someReducer,
someOtherReducer,
lastAction // <-- use it!
});
const store = createStore(rootReducer);
store.subscribe(() => {
console.log(store.getState().lastAction);
}); Still, this is not quite how Redux app is supposed to work, as this breaks the consistency in rendering. React WayIf you want to optimize a React component, optimizations described in “Advanced Performance” is what you should be after; not this. As a last resort you can always do side effects in componentWillReceiveProps(nextProps) {
if (this.props.title !== nextProps.title) {
// If somehow we're sure we can actually be faster than React at this. (not likely)
findDOMNode(this).doSomethingWith(nextProps.title);
}
} |
@gaearon i agree it isnt best way to do it in react, but when using react + other-libs to develop an app that using redux as data sync it is the best way to do it |
@gaearon @markerikson While this requirement is not needed for react and I do agree that its against the philosophy of writing code reacting to state changes rather than the event, it is important for other MVVM libraries to be able to use redux as the state management framework. Redux provides an awesome paradigm of writing all your business logic inside reducers which are pure functions, making testability, undo-redo very easy |
Can somebody please explain a little better the idea behind this? |
@matigda : please see the linked issues for prior discussion, as well as the answer for this topic in the "Design Decisions" FAQ page. |
I want to update my view in listener:
But if it possible to get last action what triggered state change, i can do some think like this:
Is it possible to get action and state in store.subscribe?
The text was updated successfully, but these errors were encountered: