What is Reselect and how does it Works in React JS ?
Last Updated :
24 Apr, 2025
In React JS, Reselect is a library that enhances the efficiency of data computation through selectors. Primarily used in conjunction with Redux, a state management library for React, Reselect facilitates the creation of memoized selectors. This article explores Reselect and its various functions, with a focus on the `createSelector` function. `createSelector` is the primary function within the Reselect library, utilized for generating memoized selectors in Redux applications.
Prerequisites:
Syntax:
import { createSelector } from 'reselect';
const mySelector = createSelector(
[input1, input2, ...],
output1, output2, ...) => {
//compute output
};
);
Steps to Create React Application And Installing Module:
Step 1: Create a React Application using the following command:
npx create-react-app reselectdemo
Step 2: After creating your project folder i.e. reselectdemo, move to it using the following command:
cd reselectdemo
Step 3: Installing the Required Packages library in your project using the following command:
npm install reselect redux react-redux
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.3",
"react-scripts": "5.0.1",
"redux": "^4.2.1",
"reselect": "^5.0.1",
"web-vitals": "^2.1.4",
}
Example 1: In the given illustration, the Counters component employs Reselect selectors, namely getSquare and getCube, to compute derived data from the Redux store's calculated value.
JavaScript
import React from "react";
import { Provider } from "react-redux";
import Counters from "./Counters";
import store from "./store";
function App() {
return (
<>
<Provider store={store}>
<Counters />
</Provider>
</>
);
}
export default App;
JavaScript
import React from 'react';
import { connect } from 'react-redux';
import { getSquare, getCube } from './selectors';
const Counter = ({ count, square, cube, increment }) => {
return (
<div>
<p>Count: {count}</p>
<p>Square: {square}</p>
<p>Cube: {cube}</p>
<button onClick={increment}>
Increment
</button>
</div>
);
};
const mapStateToProps = state => {
return {
count: state.count,
square: getSquare(state),
cube: getCube(state)
};
};
const mapDispatchToProps = dispatch => {
return {
increment: () => dispatch({ type: 'INCREMENT' })
};
};
export default connect(mapStateToProps,
mapDispatchToProps)(Counter);
JavaScript
//selectors.js
import { createSelector } from 'reselect';
const getCount = state => state.count;
export const getSquare = createSelector(
[getCount],
count => count * count
);
export const getCube = createSelector(
[getCount],
count => count * count * count
);
JavaScript
//store.js
import { createStore } from 'redux';
import counterReducer from './counterReducer';
const store = createStore(counterReducer);
export default store;
JavaScript
//counterReducer.js
const initialState = {
count: 0
};
const counterReducer =
(state = initialState, action) => {
switch (action.type) {
case 'INCREMENT':
return {
...state,
count: state.count + 1
};
default:
return state;
}
};
export default counterReducer;
Step to Run: Run the Application using the following command from the root directory of the project.
npm start
Output:
OutputExample 2: In this example, we have a list of users with their names and ages. The initial filter value is set to 30 in the App.js
component using the setFilter
action. The getFilteredUsers
selector filters the users based on their age, only including users with an age greater than or equal to the filter value.
JavaScript
import React from 'react';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store';
import App from './App';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
JavaScript
//App.js
import React, { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { setFilter } from './actions';
import { getFilteredUsers } from './selectors';
const App = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(setFilter(30));
}, [dispatch]);
const filteredUsers = useSelector(getFilteredUsers);
return (
<div>
<h1>Filtered Users:</h1>
<ul>
{filteredUsers.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
);
};
export default App;
JavaScript
//Store.js
import { createStore } from 'redux';
import userReducer from './reducers';
const store = createStore(userReducer);
export default store;
JavaScript
//reducers.js
const initialState = {
users: [
{ id: 1, name: 'John', age: 25 },
{ id: 2, name: 'Jane', age: 30 },
{ id: 3, name: 'Alice', age: 28 },
{ id: 4, name: 'Bob', age: 32 }
],
filter: 30
};
export default function userReducer(
state = initialState, action) {
switch (action.type) {
case 'SET_FILTER':
return {
...state,
filter: action.payload
};
default:
return state;
}
}
JavaScript
//actions.js
export const setFilter = filter => ({
type: 'SET_FILTER',
payload: filter
});
JavaScript
//selectors
import { createSelector } from 'reselect';
const getUsers = state => state.users;
const getFilter = state => state.filter;
export const getFilteredUsers = createSelector(
[getUsers, getFilter],
(users, filter) => {
// Apply filtering logic here
// For simplicity, let's assume
// we filter by the user's age
return users.filter(user => user.age >= filter);
}
);
Output:

Similar Reads
How to Reset a File Input in React.js ?
To reset a file input in React JS, we can use the useRef Hook to create a reference to the input element and modify its properties using the DOM API.Prerequisites:NPM & Node JSReact JSReact useRef HookApproach:To reset a file input in react we will create a reset button and handleReset function.
2 min read
How to Work with and Manipulate State in React ?
Working with and Manipulating state in React JS makes the components re-render on the UI by Updating the DOM tree. It makes sure to render the latest information and data on the interface. Prerequisites:NPM & Node.jsReact JSState in React JSReact JS Class componentsReact JS Functional Components
6 min read
How to Use react-select in React Application?
React Select is a flexible and handy dropdown library for React that handles multi-select, loading data asynchronously, custom options, and more in your components with ease. In this article, we will see how to use react-select in React Application. PrerequisiteNode.js installednpm or yarn package m
2 min read
How to Set Selected Option of a Select in React?
In the React setting the selected option of a Select can be achieved using different approaches including using the native HTML <select> element and using third-party select components like react-select. We will discuss two approaches to Setting selected options of a select in React: Table of
3 min read
How to use HOCs to reuse Component Logic in React ?
In React, making reusable components keeps your code neat. Higher-order components (HOCs) are a smart way to bundle and reuse component logic. HOCs are like magic functions that take a component and give you back an upgraded version with extra powers or information. HOCs can be implemented in a few
4 min read
Can you explain what the useState hook does in React Hooks?
The useState hook is a feature in React Hooks that allows you to add state to functional components. It creates state variables and manages their values within a functional component. Why is useState important?Before introducing hooks in React, state management was only possible in class components.
2 min read
How to use Multi-Select Dropdown in React-Bootstrap ?
In ReactJS applications, we always need to add the UI component that allows us to select multiple options from the DropDown list. So in Bootstrap, the Multi-Select Dropdown is a UI component that allows us to select multiple different options from the list of dropdown menus. Additionally, we can do
4 min read
How to use HTML <select> tag in ReactJS ?
HTML <select> tag in react is a common web development component used for dynamic form input or as a select menu. It provides multiple selection options in the form of a dropdown menu. It enables the web page to render a select box with the options. The <select> Â tag is used as an outer
2 min read
How to Disable Text Selection in ReactJS ?
In this article, we will see how to disable the text selection in ReactJS. Prerequisites:Introduction to ReactReact HooksNPM or NPXWeb applications ofteÂn provide a valuable feature called text seleÂction, enabling users to easily highlight and copy content from a webpage. However, there are situat
4 min read
What is useDeferredValue hook and how to use it?
In this article, we'll be learning about useDeferredValue hook. The `useDeferredValue` hook in React allows you to postpone updating a component of the UI. This can be beneficial for boosting performance when rendering a specific section of the UI is expensive, or when you wish to prioritize renderi
4 min read