@codeleap/utils 4.3.9 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/package.json.bak +3 -3
- package/src/react.tsx +62 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,18 +9,18 @@
|
|
|
9
9
|
"directory": "packages/utils"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "
|
|
13
|
-
"@codeleap/types": "
|
|
12
|
+
"@codeleap/config": "5.0.0",
|
|
13
|
+
"@codeleap/types": "5.0.0",
|
|
14
14
|
"ts-node-dev": "1.1.8"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "echo 'No build needed'"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@codeleap/types": "
|
|
20
|
+
"@codeleap/types": "5.0.0",
|
|
21
21
|
"axios": "^1.7.9",
|
|
22
|
-
"typescript": "5.
|
|
23
|
-
"react": "18.
|
|
22
|
+
"typescript": "5.5.2",
|
|
23
|
+
"react": "18.2.0",
|
|
24
24
|
"@tanstack/react-query": "5.60.6"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
package/package.json.bak
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@codeleap/types": "workspace:*",
|
|
21
21
|
"axios": "^1.7.9",
|
|
22
|
-
"typescript": "5.
|
|
23
|
-
"react": "18.
|
|
22
|
+
"typescript": "5.5.2",
|
|
23
|
+
"react": "18.2.0",
|
|
24
24
|
"@tanstack/react-query": "5.60.6"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
package/src/react.tsx
CHANGED
|
@@ -85,10 +85,35 @@ export function getRenderedComponent<P = any>(
|
|
|
85
85
|
return <DefaultComponent {...props} {..._props} />
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Memoizes a React functional component to prevent unnecessary re-renders.
|
|
90
|
+
*
|
|
91
|
+
* This function wraps a React functional component using `React.memo`, which provides
|
|
92
|
+
* a mechanism for memoizing the result of rendering the component. By default, it
|
|
93
|
+
* uses a comparison function that always returns `true`, indicating that the component
|
|
94
|
+
* should not re-render, regardless of prop changes. This behavior essentially freezes
|
|
95
|
+
* the component's rendering until explicitly updated.
|
|
96
|
+
*
|
|
97
|
+
* @template P - The type of the component's props.
|
|
98
|
+
* @param ComponentToMemoize - The React functional component to memoize.
|
|
99
|
+
* @returns A memoized version of the input component (`React.NamedExoticComponent`).
|
|
100
|
+
*/
|
|
88
101
|
export function memoize<P extends object>(ComponentToMemoize: React.FunctionComponent<P>): React.NamedExoticComponent<P> {
|
|
89
102
|
return React.memo(ComponentToMemoize, () => true)
|
|
90
103
|
}
|
|
91
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Checks whether a specific property in two sets of props is equal.
|
|
107
|
+
*
|
|
108
|
+
* This function compares the value of a given property (`prop`) between two objects
|
|
109
|
+
* (`prevProps` and `nextProps`) using a deep equality check (`equals`).
|
|
110
|
+
*
|
|
111
|
+
* @template P - The type of the props object.
|
|
112
|
+
* @param prop - The key of the property to compare.
|
|
113
|
+
* @param prevProps - The previous props object.
|
|
114
|
+
* @param nextProps - The next props object.
|
|
115
|
+
* @returns `true` if the values of the specified property are deeply equal; otherwise, `false`.
|
|
116
|
+
*/
|
|
92
117
|
export function memoChecker<P>(prop: keyof P, prevProps: P, nextProps: P): boolean {
|
|
93
118
|
const nextItem = nextProps[prop]
|
|
94
119
|
const prevItem = prevProps[prop]
|
|
@@ -96,6 +121,43 @@ export function memoChecker<P>(prop: keyof P, prevProps: P, nextProps: P): boole
|
|
|
96
121
|
return equals(nextItem, prevItem)
|
|
97
122
|
}
|
|
98
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Memoizes a React functional component based on specific props.
|
|
126
|
+
*
|
|
127
|
+
* This function wraps a React functional component using `React.memo` with a custom comparison
|
|
128
|
+
* function. The comparison function checks if the specified properties (passed as `check`)
|
|
129
|
+
* remain equal between renders. If all specified properties are equal, the component will not re-render.
|
|
130
|
+
*
|
|
131
|
+
* @template P - The type of the component's props.
|
|
132
|
+
* @param ComponentToMemoize - The React functional component to memoize.
|
|
133
|
+
* @param check - A single property key or an array of property keys to compare for memoization.
|
|
134
|
+
* @returns A memoized version of the input component (`React.NamedExoticComponent`).
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* import React from 'react';
|
|
138
|
+
* import { memoBy } from './utils';
|
|
139
|
+
*
|
|
140
|
+
* const MyComponent: React.FC<{ name: string; age: number }> = ({ name, age }) => {
|
|
141
|
+
* console.log('Rendering MyComponent...');
|
|
142
|
+
* return (
|
|
143
|
+
* <div>
|
|
144
|
+
* {name}, {age}
|
|
145
|
+
* </div>
|
|
146
|
+
* );
|
|
147
|
+
* };
|
|
148
|
+
*
|
|
149
|
+
* // Memoize the component based on the `name` prop.
|
|
150
|
+
* const MemoizedByName = memoBy(MyComponent, 'name');
|
|
151
|
+
*
|
|
152
|
+
* // Memoize the component based on both `name` and `age` props.
|
|
153
|
+
* const MemoizedByNameAndAge = memoBy(MyComponent, ['name', 'age']);
|
|
154
|
+
*
|
|
155
|
+
* export { MemoizedByName, MemoizedByNameAndAge };
|
|
156
|
+
*
|
|
157
|
+
* // Usage in a parent component
|
|
158
|
+
* <MemoizedByName name="Alice" age={25} />;
|
|
159
|
+
* <MemoizedByNameAndAge name="Alice" age={25} />;
|
|
160
|
+
*/
|
|
99
161
|
export function memoBy<P extends object>(
|
|
100
162
|
ComponentToMemoize: React.FunctionComponent<P>,
|
|
101
163
|
check: keyof P | Array<keyof P>
|