score:3
The following worked for me but I'm not sure if it's the best way.
import {
createSelectorCreator,
defaultMemoize,
} from 'reselect';
type IComment = { id: number };
type State = { comments: IComment[] };
type CompareFn = <T>(a: T, b: T, index: number) => boolean;
const createCustomEqualSelector = (equalFn: CompareFn) =>
createSelectorCreator(defaultMemoize, equalFn);
const selectComments = (state: State) => state.comments;
const commentsEqualFn: CompareFn = (a, b, index) =>
//need to cast it or get an error:
// Property 'length' does not exist on type 'T'
((a as unknown) as IComment[]).length ===
((b as unknown) as IComment[]).length;
export const selectEmailsFromComments = createCustomEqualSelector(
commentsEqualFn
)(
selectComments, // returns an array of comments
(comments) => {
console.log('calculating comments:', comments);
return comments;
}
);
selectEmailsFromComments({ comments: [{ id: 1 }] })
//this will log previous value because array length didn't change
console.log('memoized', selectEmailsFromComments({ comments: [{ id: 2 }] }))
Source: stackoverflow.com
Related Query
- How to create a custom equality function with reselect and Typescript?
- How to create a generic arrow function with react and typescript
- How to use TypeScript with withRouter, connect, React.Component and custom properties?
- How to test with jest and typescript with types a basic react function component
- REACT: How to create a reusable custom hook with functions and effects that can change & react to changes in a host component's data
- How to create React Context that passes a function with Typescript
- How to create a dynamic, custom React Table Component with action and status buttons
- How can I create a function with custom parameters
- How to create a signup form with React and Why won't a function be executed when called insight a form after input field?
- How create React Typescript component with required and optional values but required value without default value?
- How to create a custom useReducer with built in decision and event triggering
- how to build custom onSubmit function with formik and typescript, hooks
- How to create a modified useSelector custom hook with TypeScript
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- How to fix the error jsx no new function as prop with typescript and react?
- React Native: How do I apply a custom function to an array of objects and return the modified array in Javascript with an additional field?
- How to create forms with React and Rails?
- React prop types with TypeScript - how to have a function type?
- How to test useEffect with async function and setState inside
- How to use React with Typescript and SASS
- react, differences between component create with extend class and simple const = function
- How to use typescript with the definition of custom styles for a select using react-select
- How to create custom React Native components with child nodes
- How to Create Custom Dropdown Field Component with Redux-Form V6?
- How to use Material UI custom theme in React with Typescript
- How can I create a custom page with #react-admin without the menu sidebar like login page?
- How to use jest.spyOn with React function component using Typescript
- How to create an array of custom types typescript
- How to create a reusable react-hook-form component with TypeScript generics?
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
More Query from same tag
- Is secure to ignore the missing dependencies es-lint warning on react hooks?
- How to mock the npm module that exports the default and the object
- requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager when running android app
- How do I make a button trigger a file input onChange in React?
- REST Firebase request via qwest
- MaterialUI Custom Hover Style
- Is it possible to create a Class in React that can still access the global Redux store?
- Cannot convert event.target to string
- TypeError: _this.store.getState is not a function when using connect from Redux
- React monaco editor json schema validation event
- Wrong React hooks behaviour with event listener
- A proper way to update the state in the parent through buttons in childs
- How to customize the border color, width, and height of Material-UI Text Field with styled-components
- How to control checkBox's checked props?
- How to know when to import a package or paste it in index.html
- How to implement route path with @ symbol
- Can't understand some codes in redux-form
- Antd UI library. Overriding <Table /> behavior on empty data
- React invalid hook call error and storybook 6
- Cannot read property 'username' of undefined in react
- ReactJS - create chunks automatically with lazy loading
- AWS SDK JS v3 Get presigned url react front end
- How can I choose default checkbox at a certain value?
- React useEffect stop infinite loop
- Destructure property whose name is a reserverd keyword
- How to validate radio button values in redux forms ?
- JSX element type 'Line' is not a constructor function for JSX elements
- React not able to dynamically render correct component
- Spring boot not forwarding url path to index.html React bundle
- Uploading Images in Browser using react issue