React/Redux how to access the state in the networkservice

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


React/Redux how to access the state in the networkservice



I have created a Network service component which deals with the API call. I want to retrieve state from other components which update the store.
Im having trouble getting the state so I started using Redux, but I havent used Redux before and still trying to find a way to pass the state to the NetworkService. Any help would be great, thanks!


store


NetworkService



Here is my NetworkService.js


import RequestService from './RequestService';
import store from '../store';

const BASE_URL = 'api.example.com/';
const REGION_ID = //Trying to find a way to get the state here

// My attempt to get the state, but this unsubscribes and
// doesnt return the value as it is async
let Updated = store.subscribe(() => {
let REGION_ID = store.getState().regionId;
})

class NetworkService {

getForecast48Regional(){
let url =`${BASE_URL}/${REGION_ID }`;
return RequestService.getRequest(url)
}
}

export default new NetworkService();



store.js


import {createStore} from 'redux';

const initialState = {
regionId: 0
};
const reducer = (state = initialState, action) => {

if(action.type === "REGIONAL_ID") {
return {
regionId: action.regionId
};
}
return state;
}
const store = createStore(reducer);

export default store;



My folder heirarchy looks like this:


-App
----Components
----NetworkService
----Store





why don't you pass the value you want as an argument to getForecast48Regional
– azium
10 mins ago


getForecast48Regional




1 Answer
1



Do not import store directly. Use thunks/sagas/whatever for these reasons.



NetworkService should not know about anything below.
Thunks know only about NetworkService and plain redux actions.
Components know only about thunks and store (not store itself, but redux' selectors, mapStateToProps, mapDispatchToProps).
Store knows about plain redux actions only.



Knows - e.g. import's.


import


//////////// NetworkService.js
const networkCall = (...args) => fetch(...) // say, returns promise

//////////// thunks/core/whatever.js
import { networkCall } from 'NetworkService'

const thunk = (...args) => (dispatch, getState) => {
dispatch(startFetch(...args))
networkCall()
.then(resp => dispatch(fetchOk(...args)))
.catch(err => dispatch(fetchFail(...args)))
}

//////////// Component.js
import { thunk } from 'thunks/core/whatever'

const mapDispatchToProps = {
doSomeFetch: thunk,
}

const Component = ({ doSomeFetch }) =>
<button onClick={doSomeFetch}>Do some fetch</button>

const ConnectedComponent = connect(..., mapDispatchToProps)(Component)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

C++ virtual function: Base class function is called instead of derived