DEV Community

Discussion on: Fetching data from an api using React/Redux

Collapse
 
szulfiqar profile image
Saad Zulfiqar

Hi Markus,

Let's assume i want to use some value from store and pass it as a parameter to products API call in fetchProducts.js. How can i access that ?? Something like

fetch(https://exampleapi.com/products/${store.someVariable})

how can i access store object in fetchProducts.js ??

Collapse
 
mrhassantariq profile image
Hassan Tariq

There a few methods that can help you using state variable in thunk function:

  1. You can export the state. Create a separate .js file and export the state object by using createStore(reducerName) method, then import that state object and use the getState() method to access the store.

  2. You can directly use getState in your app-thunk function by simply passing it as an argument like return (dispatch,getState) => {} and then use this getState() method to access the store.

There are some other methods but these should work.