Blog>
Snippets

Updating the State with put Effect

Showcase using the `put` effect to dispatch an action that updates the Redux store, following a successful API call.
function* updateArticleSaga(action) {
  try {
    const response = yield call(fetch, `/api/articles/${action.articleId}`);
    const article = yield response.json();
    // Dispatch an action to update the store with the fetched article
    yield put({ type: 'ARTICLE_FETCH_SUCCEEDED', article });
  } catch (e) {
    // Dispatch an action to inform the store that the fetch failed
    yield put({ type: 'ARTICLE_FETCH_FAILED', message: e.message });
  }
}
This saga listens for an action to fetch an article. It uses the 'call' effect to perform the API request and upon success uses the 'put' effect to dispatch an action that updates the state with the fetched article. If the call fails, it dispatches a failure action.