Answer the question
In order to leave comments, you need to log in
How to load data into initialState from the database?
Friends, hello everyone.
There is a certain array of data stored in mongoDB on the one hand. There is a web application on react + redux on the other hand. How can I load an array of data from the database into initialState and only then render the application? I'm scratching my head, please help. Surely there is some standard solution to this problem. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
You create a loader (spinner or whatever) that will spin while the application is loading.
Through XHR (for example, axios, request-promise, fetch, etc.), you load the necessary data, form the initialState and push it into createStore ().
When the application is rendered, hide the loader.
Something like this.
Promise.props({
user: fetch('/user'),
products: fetch('/products')
}).then(initialState => {
const store = createStore(reducer, initialState);
render(<Provider store={store}><App /></Provider>, mountNode);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question