Blog>
Snippets

Listening for Actions with take Effect

Illustrate using the `take` effect to pause the saga until a specific action is dispatched, which triggers the next step in the saga.
import { take, put, call } from 'redux-saga/effects';

function* authenticateUser() {
  // Placeholder for authentication logic
  console.log('User authenticated');
}

function* watchLoginActions() {
  while (true) {
    // Waiting for a LOGIN_REQUEST action to be dispatched
    yield take('LOGIN_REQUEST');
    // Once LOGIN_REQUEST is caught, authenticateUser saga is called
    yield call(authenticateUser);
    // After authentication, you could dispatch a success action
    // yield put({type: 'LOGIN_SUCCESS'});
  }
}
This code snippet illustrates how the `take` effect is used to pause the saga until a 'LOGIN_REQUEST' action is dispatched to the Redux store. Once this action is observed, `authenticateUser` saga is executed to handle user authentication. This pattern is commonly used for handling asynchronous workflows such as user login processes in Redux-Saga.