Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

"...because actions have access to app state." #698

Closed
jdart opened this issue Jan 28, 2016 · 5 comments
Closed

"...because actions have access to app state." #698

jdart opened this issue Jan 28, 2016 · 5 comments

Comments

@jdart
Copy link

jdart commented Jan 28, 2016

In the README there's a quote "And remember, we have to pass only data going to be rendered, because actions have access to app state." I saw that you have set up actions to benefit from DI, but does that mean you can inject state?

@AlesJiranek
Copy link
Contributor

Yes. You can get state from Store via getState() function

({store: {getState}}) => {
   const state = getState();
}

@jdart
Copy link
Author

jdart commented Jan 29, 2016

Awesome, there are several examples in the project, thanks for humouring me.

@jdart jdart closed this as completed Jan 29, 2016
@AugustinLF
Copy link
Contributor

Actually, with the injectDependencies middleware, you can simply do:

export function myAction() {
    return ({dispatch, getState}) => {
        const myState = getState();
        // Do stuff with state
        dispatch({type: MY_ACTION});
    };
}

This is pretty similar to the way redux-thunk works

@svrcekmichal
Copy link

@AugustinLF actually i believe you should only read from state, not do stuff with state as you say in comment, for doing stuff you should dispatch action. Only little problem with this injectDependencies I can see is in my opinion it's too late in middleware to check if something is loaded and stoping the action

@AugustinLF
Copy link
Contributor

Yes of course. When I was saying "do something with", it was "read some value in it". Because anyway, event if you do change values, If you use ImmutableJS, it won't do anything.

Yes it's true that its a bit late to prevent from firing an action, but if you want to do a check before calling your action creator, I think you'd be better off with this part of the state in the component, and simply do the check on the components props.

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

No branches or pull requests

4 participants