0

I wish to initialise a state (list) from a API endpoint.

What's the best way of doing this? For example, would you supply a starting state into the reducer, or would the starting state in the reducer be empty and somehow have an action for the new state?

I am considered using redux-thunk to do this sort of thing, but a simple example of how to do the above would be great.

let startState = {}; 
//some form of URL fetch here or supply data through action?

const reducer = (state = startState, action) => {
    ...
}
vogomatix
  • 4,517
  • 2
  • 16
  • 38
  • 1
    possible duplicate of [Async redux thunk](https://stackoverflow.com/questions/37435421/async-initial-state-react-redux-redux-thunk) - In short: not the best way to go about this. Load the state when the application needs it. – Tikkes Jul 28 '17 at 09:27
  • 1
    You need to call an action that updates the the makes an API call and then update update the store in reducer rather than initializzing it , you can refer this question to use redux thunk https://stackoverflow.com/questions/45341206/actions-must-be-plain-javascript-objects/45343435#45343435 – Shubham Khatri Jul 28 '17 at 10:17
  • @ShubhamKhatri - I believe that is what I was looking for - cheers – vogomatix Jul 28 '17 at 12:13
  • @vogomatix consider upvoting the answers that help you, it increases their visibility and may help others as well – Shubham Khatri Jul 28 '17 at 12:39
  • I upvoted yours already :) – vogomatix Jul 28 '17 at 12:44

0 Answers0