1

I have a Redux middleware that requires some data to be configured via a server call (eg, fetch) which is async / requires promises.

However, every example of createStoresuch as this one seems to use a Singleton pattern for the store. This means that the store gets initialized before my fetch is complete. Because it's middleware, I can't "reconfigure" it after createStore is called.

How can i configure createStore without using a singleton pattern to configure middleware?

1 Answer 1

1

How do you fetch those data? If it's just a simple API call. You can easily wait for the data to be returned then pass the data to createStore. Something like this:

const fetchData = () => {
  return Promise.resolve({
    data: 'Your data here'
  });
}

const initialState = window.__INITIAL_STATE__;
fetchData()
  .then((data) => {
    initialState.somethingYouNeed = data;
    const store = createStore(initialState);
    // Do the rest here
  });
Sign up to request clarification or add additional context in comments.

2 Comments

I am using react-router-redux and it requires a reference to the store to call syncHistoryWithStore. How would I get that store reference to my router? Most cases rely on the singleton implementation to do this.
Currently, I'm follow this example. Still there are quite a lot issues in the example. But it's a very nice boilerplate. You can find the answer in. github.com/erikras/react-redux-universal-hot-example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.