-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathscratchpad
More file actions
25 lines (20 loc) · 792 Bytes
/
Copy pathscratchpad
File metadata and controls
25 lines (20 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Access the Redux Store Outside a React Component - https://daveceddia.com/access-redux-store-outside-react/
Example: error handling with async/await in React or React Thunk
https://redux.js.org/usage/writing-logic-thunks
function fetchData(someValue) {
return async (dispatch, getState) => {
dispatch(requestStarted())
// Have to declare the response variable outside the try block
let response
try {
response = await myAjaxLib.post('/someEndpoint', { data: someValue })
} catch (error) {
// Ensure we only catch network errors
dispatch(requestFailed(error.message))
// Bail out early on failure
return
}
// We now have the result and there's no error. Dispatch "fulfilled".
dispatch(requestSucceeded(response.data))
}
}