for more information see docs for take(pattern)
Spawns a saga
on each action dispatched to the Store that matches
pattern
. And automatically cancels any previous saga
task started
previously if it's still running.
Each time an action is dispatched to the store. And if this action matches
pattern
, takeLatest
starts a new saga
task in the background. If a
saga
task was started previously (on the last action dispatched before the
actual action), and if this task is still running, the task will be
cancelled.
In the following example, we create a basic task fetchUser
. We use
takeLatest
to start a new fetchUser
task on each dispatched
USER_REQUESTED
action. Since takeLatest
cancels any pending task started
previously, we ensure that if a user triggers multiple consecutive
USER_REQUESTED
actions rapidly, we'll only conclude with the latest action
import { takeLatest } from redux-saga/effects
function* fetchUser(action) { ... }
function* watchLastFetchUser() { yield takeLatest('USER_REQUESTED', fetchUser) }
takeLatest
is a high-level API built using take
and fork
. Here is how
the helper could be implemented using the low-level Effects
const takeLatest = (patternOrChannel, saga, ...args) => fork(function*() { let lastTask while (true) { const action = yield take(patternOrChannel) if (lastTask) { yield cancel(lastTask) // cancel is no-op if the task has already terminated } lastTask = yield fork(saga, ...args.concat(action)) } })
for more information see docs for take(pattern)
arguments to be passed to the started task. takeLatest
will add
the incoming action to the argument list (i.e. the action will be the last
argument provided to saga
)
Spawns a saga
on each action dispatched to the Store that matches
pattern
. And automatically cancels any previous saga
task started
previously if it's still running.
Each time an action is dispatched to the store. And if this action matches
pattern
, takeLatest
starts a new saga
task in the background. If a
saga
task was started previously (on the last action dispatched before the
actual action), and if this task is still running, the task will be
cancelled.
In the following example, we create a basic task fetchUser
. We use
takeLatest
to start a new fetchUser
task on each dispatched
USER_REQUESTED
action. Since takeLatest
cancels any pending task started
previously, we ensure that if a user triggers multiple consecutive
USER_REQUESTED
actions rapidly, we'll only conclude with the latest action
import { takeLatest } from redux-saga/effects
function* fetchUser(action) { ... }
function* watchLastFetchUser() { yield takeLatest('USER_REQUESTED', fetchUser) }
takeLatest
is a high-level API built using take
and fork
. Here is how
the helper could be implemented using the low-level Effects
const takeLatest = (patternOrChannel, saga, ...args) => fork(function*() { let lastTask while (true) { const action = yield take(patternOrChannel) if (lastTask) { yield cancel(lastTask) // cancel is no-op if the task has already terminated } lastTask = yield fork(saga, ...args.concat(action)) } })
for more information see docs for take(pattern)
Spawns a saga
on each action dispatched to the Store that matches
pattern
. And automatically cancels any previous saga
task started
previously if it's still running.
Each time an action is dispatched to the store. And if this action matches
pattern
, takeLatest
starts a new saga
task in the background. If a
saga
task was started previously (on the last action dispatched before the
actual action), and if this task is still running, the task will be
cancelled.
In the following example, we create a basic task fetchUser
. We use
takeLatest
to start a new fetchUser
task on each dispatched
USER_REQUESTED
action. Since takeLatest
cancels any pending task started
previously, we ensure that if a user triggers multiple consecutive
USER_REQUESTED
actions rapidly, we'll only conclude with the latest action
import { takeLatest } from redux-saga/effects
function* fetchUser(action) { ... }
function* watchLastFetchUser() { yield takeLatest('USER_REQUESTED', fetchUser) }
takeLatest
is a high-level API built using take
and fork
. Here is how
the helper could be implemented using the low-level Effects
const takeLatest = (patternOrChannel, saga, ...args) => fork(function*() { let lastTask while (true) { const action = yield take(patternOrChannel) if (lastTask) { yield cancel(lastTask) // cancel is no-op if the task has already terminated } lastTask = yield fork(saga, ...args.concat(action)) } })
for more information see docs for take(pattern)
arguments to be passed to the started task. takeLatest
will add
the incoming action to the argument list (i.e. the action will be the last
argument provided to saga
)
Spawns a saga
on each action dispatched to the Store that matches
pattern
. And automatically cancels any previous saga
task started
previously if it's still running.
Each time an action is dispatched to the store. And if this action matches
pattern
, takeLatest
starts a new saga
task in the background. If a
saga
task was started previously (on the last action dispatched before the
actual action), and if this task is still running, the task will be
cancelled.
In the following example, we create a basic task fetchUser
. We use
takeLatest
to start a new fetchUser
task on each dispatched
USER_REQUESTED
action. Since takeLatest
cancels any pending task started
previously, we ensure that if a user triggers multiple consecutive
USER_REQUESTED
actions rapidly, we'll only conclude with the latest action
import { takeLatest } from redux-saga/effects
function* fetchUser(action) { ... }
function* watchLastFetchUser() { yield takeLatest('USER_REQUESTED', fetchUser) }
takeLatest
is a high-level API built using take
and fork
. Here is how
the helper could be implemented using the low-level Effects
const takeLatest = (patternOrChannel, saga, ...args) => fork(function*() { let lastTask while (true) { const action = yield take(patternOrChannel) if (lastTask) { yield cancel(lastTask) // cancel is no-op if the task has already terminated } lastTask = yield fork(saga, ...args.concat(action)) } })
Spawns a
saga
on each action dispatched to the Store that matchespattern
. And automatically cancels any previoussaga
task started previously if it's still running.Each time an action is dispatched to the store. And if this action matches
pattern
,takeLatest
starts a newsaga
task in the background. If asaga
task was started previously (on the last action dispatched before the actual action), and if this task is still running, the task will be cancelled.Example
In the following example, we create a basic task
fetchUser
. We usetakeLatest
to start a newfetchUser
task on each dispatchedUSER_REQUESTED
action. SincetakeLatest
cancels any pending task started previously, we ensure that if a user triggers multiple consecutiveUSER_REQUESTED
actions rapidly, we'll only conclude with the latest actionimport { takeLatest } from
redux-saga/effects
function* fetchUser(action) { ... }
function* watchLastFetchUser() { yield takeLatest('USER_REQUESTED', fetchUser) }
Notes
takeLatest
is a high-level API built usingtake
andfork
. Here is how the helper could be implemented using the low-level Effectsconst takeLatest = (patternOrChannel, saga, ...args) => fork(function*() { let lastTask while (true) { const action = yield take(patternOrChannel) if (lastTask) { yield cancel(lastTask) // cancel is no-op if the task has already terminated } lastTask = yield fork(saga, ...args.concat(action)) } })