Blog>
Snippets

Optimizing Performance with `select` and `take`

Showcase the use of `select` and `take` effects in a non-blocking manner to optimize the performance of state selection and event listening within Redux Saga.
import { select, take, put } from 'redux-saga/effects';

function* optimizedSelectorSaga() {
  while (true) {
    // Wait for a specific action to be dispatched
    yield take('ACTION_TYPE_TO_LISTEN_FOR');
    // Use select to efficiently retrieve a piece of state
    const selectedData = yield select(state => state.path.to.selectedData);
    // Work with the selected data, for example, dispatch an action
    yield put({ type: 'USE_SELECTED_DATA', payload: selectedData });
  }
}
This saga efficiently listens for a specific action and then uses the select effect to retrieve a portion of the state. It operates in a non-blocking manner as it waits for the action, optimizes state selection, and continues listening.