r/learnreactjs • u/dontspookthenetch • Jun 15 '23
Question Help for best practice with a simple problem that is more complex that it appears
Long story short, I have a third party service context provider that accepts some parameters as props, take the user out of the application or some authentication, and brings the user back when authenticated to the application. Previously all parameters were known in advance so this was not an issue. This provider, let's just call it Provider for simplicity, wraps the App itself.
What needs to happen now is that the user needs to type some values in a login field, those values need to be stored in Redux, the Provider needs to observe those values and then call out to the service with the updated information.
I am having some issues, however. Once the fields are completed, the user submits the login form, which dispatches an action to the store with the new parameters.
In the Provider, I have a Redux selector and I observe those parameters, let's just call one ParamaterA for an example, in a useEffect. When ParamaterA updates, the Effect runs and the Provider will (theoretically) will run the call to the service with the updated parameter
What is happening though is that the Provider runs with the default ParamterA seemingly before it gets the value from Redux
What is the best way to approach this situation?
2
u/Jerp Jun 15 '23
1
u/Ok_Marsupial5008 Jun 17 '23
Bit of a side note, but if your service call always happens in response to Redux store actions, and the service then updates some Redux state, you could check out Redux listener middleware instead of using useEffect in the Provider. It's essentially like useEffect but it can listen for specific Redux actions/store changes, and has a bunch of functionality built in for handling async tasks etc.