How to access the reaction reduct storage outside the component

I have a file that exports various utility functions for use through components, and these functions must access the reduction state. How to import a state object into this file?

+6
source share
3 answers

connect does not work here if your utility functions are not react elements.

The best idea is to create an import repository and then use the getState function,

 import store from 'store/createStore'; const state = store.getState(); 
+3
source

Well, this is not an easy answer, but after too much research, I found them, which are the only 2 articles that explain anything. They explain how to access the repository directly outside the Component (if necessary), and also mention the philosophy of pure functions / functional programming, as well as potential performance problems when directly connecting non-component functions to the repository. Personally, I went with @anoop and passed the parameters around in one object as deep as necessary.

For a direct connection (which receives storage from this.context, as connect () does, see the discussion here and, in particular, the gaearon comment of September 16, 2015 and September 22, 2015. It seems that this access can be achieved through connect ()

For a little read on functional programming / pure functions see discussion here

+1
source

The utility should receive a state as an argument.

Since you want to use the utility in components (views), you can save the state in a member variable of your smart component (the one that uses the connect() function) via the mapStateToProps(state) callback, then you can pass this member to your silent components.

0
source

All Articles