score:0
why not pass the id as the variable in your request?
//is user null or not initialized?
sendinforequest(user?.id);
sendsalesrequest(user?.id);
there's nothing wrong with sending something explicitly--especially in a asynchronous context (it is also easier to test).
i think the bigger issue is your state might not be what you expect (like null). in my experience it makes the most sense to keep things as simple an explicit as possible.
in response to the comment i have some trouble understanding what you're asking, but if i understand the logic you basically want to do:
-> send request -> get id -> use id
fundamentally that can't be done up front without knowing the id. what you probably want is something like:
-> send request (wait)
-> with data do {
action1, action2, etc...
}
there's not enough code to give you any real information beyond that. if the user id doesn't exist in your state you need to request and use it. in redux that usually looks something like
//and please forgive me, there are a lot of different ways to write this
...
const doafteruseridexists = (userid) => {
dispatch(a)
dispatch(b)
...
dispatch(x)
}
dispatch( initialaction(doafteruseridexists) )
//--in the backend
export const initialaction = (callback) => {
return dispatch => {
//do some business logic
...
const user = new user()//id is created
if(callback) {
callback(user)
}
dispatch({
type: cascade_user_function,
user: user,
})
}
}
it isn't that different from what you're doing, except it has a linear flow. promise.all()
also isn't viable because it will run all your events at the same time (pointless, you need an id first).
this isn't a perfect solution, but it gives you an idea of how to control the flow of data. you could also investigate sagas or other patterns to make "thunks" work. alternatively you could flip it so that the "sub logic" like posting info and sales requests happens in the back-end if they're part of a transaction.
it isn't magic, you need to find a solution that works for you. i tend to lean on callbacks because they are a linear flow, but there are many different patterns. i find this one the easiest to read.
Source: stackoverflow.com
Related Query
- How to resolve async chaining using thunk in redux
- How do I resolve 'Property 'type' is missing in type 'AsyncThunkAction' using Redux Toolkit (with TypeScript)?
- What are the benefits of using thunk middleware in redux over using regular functions as async action creators?
- How to indicate "loading" state for an async action during first render using redux
- Redux Toolkit w/ TypeScript: how can I properly type the return value from an async thunk
- How to trap HTTP 4xx errors when using Redux Toolkit async REST API pattern?
- how to modify the state of another slice from pending state of redux toolkit async thunk
- How to Chain Dynamic Series of Async Actions Using Redux Thunk?
- How redux thunk calls curried async thunk functions?
- How to dispatch an action just if another action was correctly dispatched ? Using redux thunk (react)
- How do you make an async request to firebase `once` using redux observable?
- Understanding async dispatch actions using redux thunk
- how to write test cases for redux async actions using axios?
- How To Make A Post Request Using Redux Thunk With Redux Toolkit
- How to dispatch async action in Redux using Redux Thunk: TypeError: Cannot read property 'loading' of undefined
- How to stub async action in componentDidMount for react redux connected component test using Jest
- How to fire periodic actions using setTimeout and dispatcher in redux
- How to reset state of Redux Store when using configureStore from @reduxjs/toolkit?
- How to return a promise from an action using thunk and useDispatch (react-redux hooks)?
- Testing async actions with redux thunk
- How to reduce renders using redux + normalizr
- chaining multiple async dispatch in Redux
- How to handle focus using declarative/functional style libraries like Redux and ReactJS?
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- How to get the changed state after an async action, using React functional hooks
- How to unit test async Redux actions to mock ajax response
- How to execute store.unsubscribe from useEffect using React hooks and Redux
- How to use redux thunk to redirect after success or error?
- How to make Redux Thunk and Apollo GraphQL work together
- Redux thunk: how to await completion of an async action
More Query from same tag
- Material UI Icon created from document.createElementNS in React is not loading correctly
- Interference in data retrieval
- React Router `browserHistory`: Do I have to render on server?
- Render Items from useState Hook React
- TypeError: Cannot read properties of undefined (reading 'filter') when array is coming from a get api
- Access String array in Json
- React setstate not merging the old state into the new state
- renderProps and TypeScript
- React passing component with props and decorate it
- React Table: resetting filters programmatically
- Getting values from a text editor in React js
- how to convert JSON to XML with Nodejs?
- React-router check if can go back
- Apollo client issues in next.js , javascript, graphql, react js
- ReactJS and images in public folder
- React: avoid unnecessary re-renders of child components (with callback prop) when parent re-render too often
- State value `undefined` on state onChange
- AutoFocus on Text Input in React Using Styled Components
- Material-UI Error: Object is not a function within makeStyles?
- How to customize the border color, width, and height of Material-UI Text Field with styled-components
- TypeError: Employees.find is not a function from meteor withTracker
- React Router - Route not working on Refresh in webpack production build
- Link by id in React JS
- React onSubmit passing props
- CSS styles are not being applied to React file
- reactjs Twilio video chat - appending video as remote media
- Delay Auth0 redirect in React until after user credentials have been received
- How to update react state from react-select multi?
- React history push data as parameter to page
- How to unsubscribe from setinterval only when input updated