ReasonJun

Redux : toolkit : abort() 본문

Frontend/Redux

Redux : toolkit : abort()

ReasonJun 2023. 8. 20. 02:12
728x90

abort is a function in Redux Toolkit that can be used to cancel an asynchronous action. Asynchronous actions are actions that perform some kind of external operation, such as fetching data from an API.

 

abort takes two arguments:

  • action: The action to be aborted.
  • error: An optional error object to be passed to the rejected action.

The action argument is the action that you want to cancel. The error argument is an optional error object that will be passed to the rejected action.

 

For example, the following code cancels the fetchUsers action if the user cancels the request:

const fetchUsers = createAsyncThunk('fetchUsers', async () => {
  const response = await fetch('<https://api.example.com/users>');
  const users = await response.json();
  return users;
});

const cancelFetchUsers = () => {
  abort(fetchUsers, new Error('User canceled request'));
};

The cancelFetchUsers function will cancel the fetchUsers action and dispatch the fetchUsersRejected action with the error object 'User canceled request'.

 

The abort function is a powerful tool that can help you to manage asynchronous actions in Redux. It is recommended for most projects.

 

Here are some of the benefits of using abort:

  • It allows you to cancel asynchronous actions that are no longer needed.
  • It helps to prevent memory leaks.
  • It makes it easier to manage large state trees.

If you need to cancel asynchronous actions in Redux, I recommend using abort. It is a powerful tool that can help you to write more concise and maintainable Redux code.

728x90

'Frontend > Redux' 카테고리의 다른 글

Redux: PersistGate  (0) 2023.08.20
Redux : toolkit : persistReducer & persistStore  (0) 2023.08.20
Redux : createAsyncThunk  (0) 2023.08.20
Redux : toolkit : createAction / createReducer  (0) 2023.08.19
Redux : toolkit API : configureStore()  (0) 2023.08.19
Comments