I searched a bit about this question but found very vague answers. In redux, we know that the state is stored as an object. But where is this state stored actually? Is it somehow saved as a file which can be accessed by us later on? What I know is that it does not store it in a cookie format or in the browser's local storage.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
States are stored in redux-store. Redux Store is a global store which can be accessed anywhere/any components.
Let consider an example of getting Index of data using third party API. The following snippet uses
componentWillMount
which will trigger a fetch call using redux action.The above snippet will fetch index of data using a redux action. The below code is a redux action,
Redux action will dispatch data to reducer, where state will be initialized in redux store. The following code snippet is redux-reducer
State stored in redux store will be mapped using function
mapStateToProps
, implemented in the above component. Now you can access the state using props in the respective component. LifecyclehookcomponentWillReceiveProps
will be able to fetch the state stored redux store.You can access the State by means of using
store.getState()
in any component.The only drawback of using reducer state, is that it will reset the state when you refresh the component/application. Go through Reducer Store , for more information.The state in Redux is stored in memory, in the Redux store.
This means that, if you refresh the page, that state gets wiped out.
You can imagine that store looking something like this:
The state in redux is just a variable that persists in memory because it is referenced (via closure) by all redux functions.
Here's a simplified example of what is going on:
Furthermore, the statement
Isn't correct. State in redux can be any valid javascript value, not just an object. It just makes most sense for it to be an object (or a special object like an array) because that allows for a more flexible data structure (but you could make the state just be a number for example, if you wanted to).
Check out the actual Redux implementation for more details.
If you want the state to persist in a cookie or localStorage, you would enhance the store such that, on top of updating the state in memory, it will save to your desired storage as well (and load from that storage when the app loads)