Automated summary
Selector functions colocated with each slice that return derived view-data from saga state shape, including filter/sort projections combining multiple slices.
Strengths
- Colocation with slice provides a clear API boundary and reduces coupling.
- Pure functions, easy to test.
- Memoization with reselect available when needed.
- Naming conventions improve readability and consistency.
Limitations
- Can introduce redundancy with pass-through selectors.
- Over-memoization may cause memory leaks if not handled carefully.
- Requires discipline to keep selectors stateless.
When building React apps with Redux Saga and needing clean separation of state shape from view logic.
For very simple state where direct access is fine, or when using a different state management pattern like React Context.
Security analysis
SafeThe skill is a purely instructional guide for writing selector functions in a React-Redux application. It contains no executable commands, network access, file manipulation, or any dangerous operations. It is safe for listing.
No concerns found
How this analysis is madeExamples
Create colocated selectors for a todo slice including isLoading, getTodoItems, getCurrentTodoItem, and a cross-slice getVisibleTodos that combines with a filter slice.Write selectors for a filter slice with getFilterId and derived isFilterActive.Refactor the getVisibleTodos selector to use reselect's createSelector for memoization, ensuring it only recomputes when todoItems or filterId change.name: state-selectors
description: Selector functions for the chota-react-saga template — colocated with each slice, return derived view-data from the saga state shape (isLoading/isContentLoading/isActionLoading/error/<items>/<current>), filter/sort projections that combine slices (e.g. visibleTodos × selectedFilter).
when_to_use: Writing selectXxx/getVisibleXxx helpers colocated with a slice; deriving filtered or aggregated lists from <items> and the filter slice; replacing raw state.x.y access in containers.
paths:
- "/state//*.selectors.{js,ts}"
Selectors (Saga template)
What is a Selector?
A selector is a pure function (state) => derivedValue that hides the state shape from consumers. Containers call selectors with useSelector(selector); components only see the projected view-data, never the slice's internal flags or normalisation tricks.
Key Principles
-
Colocate selectors with their slice.
<slice>.selectors.jslives next to<slice>.reducer.js. Cross-slice selectors live where the primary slice lives (e.g.getVisibleTodoslives intodo.selectors.jsbecause that's where the items list lives, even though it also readsfilters). -
One selector per derived shape. A selector returns either a primitive (
isLoading), a slice ref (state.todo), a list (state.todo.todoItems), or a derived projection (getVisibleTodos). Don't fuse unrelated derivations into one selector. -
Pass-through selectors are not boilerplate, they're API.
getTodoItems = (state) => state.todo.todoItemslooks redundant but it's the contract: tomorrow you can normalise the slice or rename the field, and only this file changes. -
Cross-slice selectors take both slices as args. The
getVisibleTodos(todoSlice, filterId)pattern keeps the selector pure and easily testable; the container picks the slices viauseSelectorand passes them in. -
Memoise only when measured. Plain selectors are cheap. Reach for
createSelector(reselect) when a selector returns a NEW array/object on every call AND that result is consumed by a memoised component — otherwise you'll see needless re-renders.
Best Practices
✅ DO:
- Name boolean flag selectors
is…(isLoading,isContentLoading). - Name list selectors
get<Plural>and projectionsget<Verb><Plural>(e.g.getVisibleTodos). - Keep selectors stateless: no module-level caches; if you need memoisation, use reselect.
❌ DON'T:
- Don't fetch inside selectors. Sagas do that.
- Don't read
localStorageorDate.now()from a selector. Tests will fail nondeterministically. - Don't return
stateitself — projects always start one level in (state.todo).
Code Patterns
Slice selectors (todo.selectors.js)
// Saga state shape:
// { isLoading, isActionLoading, isContentLoading, error,
// todoItems[], currentTodoItem, previousStateTodoItems? }
// Pass-through projections — the API for downstream consumers.
export const getTodo = (state) => state.todo;
export const getTodoItems = (state) => state.todo.todoItems;
export const getCurrentTodoItem = (state) => state.todo.currentTodoItem;
// Lifecycle flags (the UI picks one to render against).
export const isContentLoading = (state) => state.todo.isContentLoading;
export const isActionLoading = (state) => state.todo.isActionLoading;
export const getError = (state) => state.todo.error;
// Cross-slice: takes the slice + the filter id, returns the view list.
// (the container picks both via useSelector and calls this in render).
export const getVisibleTodos = (todoSlice, filterId) => {
const items = todoSlice.todoItems;
switch (filterId) {
case "SHOW_COMPLETED": return items.filter((t) => t.completed);
case "SHOW_ACTIVE": return items.filter((t) => !t.completed);
case "SHOW_ALL":
default: return items;
}
};
Filter slice selectors (filters.selectors.js)
// The filter slice is an array of { id, label, selected }.
export const getSelectedFilter = (state) =>
state.filters.find((f) => f.selected) || state.filters[0];
export const getFilters = (state) => state.filters;
Calling from a container
import { useSelector } from "react-redux";
import { getSelectedFilter } from "../state/filters/filters.selectors";
import { getVisibleTodos } from "../state/todo/todo.selectors";
const selectedFilter = useSelector(getSelectedFilter);
const todoData = useSelector((state) =>
getVisibleTodos(state.todo, selectedFilter.id)
);
When to add createSelector
// Reach for reselect when a derived projection is hot AND consumed by
// a memoised component. Most slices don't need this.
import { createSelector } from "reselect";
const selectTodo = (state) => state.todo;
const selectFilter = (state, filterId) => filterId;
export const makeGetVisibleTodos = createSelector(
[selectTodo, selectFilter],
(todoSlice, filterId) => /* …same logic as above… */
);
Related Terminologies
- Reducer — owns the state shape selectors project from.
- Container — the only call-site of
useSelector(selector). - Filters — the cross-slice friend of
getVisibleTodos.
Quality Gates
- [ ] One selectors file per slice; no mixing.
- [ ] Pass-through selectors exist for every consumed field.
- [ ] No side effects (no fetch, no
Math.random, noDate.now). - [ ] Cross-slice selectors take slice args, not
state. - [ ]
createSelectoronly used where memoisation is measurably needed.
Source: templates/chota-react-saga/src/state/todo/todo.selectors.js, filters.selectors.js
From the file to a first result
The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.
View the source and supporting filesThis download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.
1. Download the file
Download SKILL.mdFrom the root of a test project, save the file as .skill-downloads/state-selectors-saga-template-mryiqj0u/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.
Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.
mkdir -p '.skill-downloads/state-selectors-saga-template-mryiqj0u' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/state-selectors-saga-template-mryiqj0u/download' --output '.skill-downloads/state-selectors-saga-template-mryiqj0u/SKILL.md.part' && mv '.skill-downloads/state-selectors-saga-template-mryiqj0u/SKILL.md.part' '.skill-downloads/state-selectors-saga-template-mryiqj0u/SKILL.md'2. Check prerequisites
Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.
No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.
The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.
3. Load the skill
After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.
OpenSkills
OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.
Official OpenSkills installationopenskills install './.skill-downloads/state-selectors-saga-template-mryiqj0u'openskills read 'state-selectors-saga-template-mryiqj0u'Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.
For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.
openskills syncManual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.
4. Try a first task
Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:
Read .skill-downloads/state-selectors-saga-template-mryiqj0u/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.
React Hook Form Best Practices
Development
Master React Hook Form: avoid anti-patterns, manage subscriptions, and handle reset, dirty state, and controlled inputs correctly.
Emotion to Mantine Migration
Development
Migrate Emotion styled-components to Mantine components with style props and CSS modules. Use when converting .styled.tsx files or removing @emotion imports.
Scope Creep Detector
Development
Analyzes git diffs against a stated intent to detect scope creep, unrelated files, broad PRs, and oversized changes, then suggests keep, split, or justify actions.