O
O
Ostic2019-05-12 14:47:44
redux
Ostic, 2019-05-12 14:47:44

How to pass data (payload) to redux-saga?

Hello.
There is an example from documentation:

The code
export function* incrementAsync() {
  yield delay(1000)
  yield put({ type: 'INCREMENT' })
}
export function* watchIncrementAsync() {
  yield takeEvery('INCREMENT_ASYNC', incrementAsync)
}
how to transfer data? to intercept, we intercept and then initiate a synchronous action, but how to receive and send the "load"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-05-12
@Ostic

A simple query example:

export function* fetchPostBySlugSaga({ payload: { slug } }) {
  try {
    const data = yield call(Api.fetchPostBySlug, slug);
    yield put(fetchPostBySlugSucceeded(data));
  } catch (error) {
    yield put(fetchPostBySlugFailed(error));
  }
}

export default function* watchFetchPostBySlugSaga() {
  yield takeLatest(FETCH_POST_BY_SLUG, fetchPostBySlugSaga);
}

Actions:
export function fetchPostBySlug(slug) {
  return {
    type: FETCH_POST_BY_SLUG,
    payload: {
      slug,
    },
  };
}

export function fetchPostBySlugSucceeded(data) {
  return {
    type: FETCH_POST_BY_SLUG_SUCCEEDED,
    payload: {
      data,
    },
  };
}

export function fetchPostBySlugFailded(error) {
  return {
    type: FETCH_POST_BY_SLUG_FAILED,
    payload: {
      error,
    },
  };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question