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
- Pass an object value in props to another component
- Low performance in React Native with Redux
- Add a search feature in React
- Set Recat LInk to Button when button click call the function & after Navigite to Another Component
- Sort by date with comparison, returns wrong order in React
- React: Passing state from child to child to parent
- react I want to hide the y-axis values and tooltip in the react chart
- Testing a component which calls an async function
- .NET Core 3.1 with ReactJs 16.9.0 - Run in IIS sub-folder
- Tailwind mixed classes with utility classes
- React + useState() + previousState
- How do i turn a nested JSON object to an array Spring boot?
- Component not rendering while using route in ReactJS
- Access variable made in componentDidMount
- How can i split a column of list in different rows in bootstrap
- Unable to position content to be center in flexbox
- Update state through an ajax request but state is still undefined
- In react-slick, className doesn't match with class names in CSS files converted by webpack css-loader
- Firestore - Web version 9 - get all documents in collection whose id is in array of ids
- ref.current is null in react
- React Typescript facing issue with enzyme
- How to use Segment's Visual Tagger with a React app in localhost
- Easier way to to disable link in React?
- How to upload multiple Image files to the server using Formidable and React Js
- Why? Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'
- How to integrate next-i18next, nextjs & locize
- How to add/remove in Array inside dynamic Object
- How do you use Library Constructer outside of a useEffect hook?
- Jest, Enzyme Test multiple occurrence of the same child component
- Print: Entry, ":CFBundleIdentifier", Does Not Exist