-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from XantreGodlike/fix-nested-memos
Fixed nested memos
- Loading branch information
Showing
10 changed files
with
160 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
"react-fast-hoc": patch | ||
--- | ||
|
||
Fixed wrapping into hoc in case of nested `React.memo` | ||
Resolves: [#17](https://github.com/XantreGodlike/react-fast-hoc/issues/17) | ||
|
||
```tsx | ||
const Component = transformProps( | ||
React.memo( | ||
React.memo((props) => { | ||
console.log(props); // prev: { c: 30 }, now: { a: 10, b: 20, c: 30 } | ||
return null; | ||
}) | ||
), | ||
(props) => ({ ...props, a: 10, b: 20 }) | ||
); | ||
|
||
root.render(<Component c={30} />); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...-fast-hoc/src/mimicToNewComponent.test.ts → ...src/__tests__/mimicToNewComponent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 14 additions & 16 deletions
30
packages/react-fast-hoc/src/react.test.ts → ...eact-fast-hoc/src/__tests__/react.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-fast-hoc/src/rename.test.ts → ...act-fast-hoc/src/__tests__/rename.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...fast-hoc/src/handlers/rewriteCall.test.ts → ...ast-hoc/src/__tests__/rewriteCall.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
RenderResult, | ||
render, | ||
} from "@testing-library/react"; | ||
import React, { ComponentType, createElement } from "react"; | ||
|
||
export const renderComponent = (Component: React.ComponentType): RenderResult => | ||
render(createElement(Component)); | ||
|
||
export const lazyShort = <T extends ComponentType<any>>(component: T) => | ||
React.lazy(() => | ||
Promise.resolve({ | ||
default: component, | ||
}) | ||
); | ||
|
||
export const applyHocs = <T extends ComponentType<any>>( | ||
Component: T, | ||
hocs: ((...args: any) => any)[] | ||
): T => hocs.reduceRight((acc, cur) => cur(acc), Component); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// https://github.com/facebook/react/blob/b9244f79bab133e0e6d2e992b5be4722b4ac7536/packages/shared/shallowEqual.js | ||
const is = Object.is; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
|
||
/** | ||
* Performs equality by iterating through keys on an object and returning false | ||
* when any key has values which are not strictly equal between the arguments. | ||
* Returns true when the values of all keys are strictly equal. | ||
*/ | ||
export function reactShallowEqual(objA: unknown, objB: unknown): boolean { | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
|
||
if ( | ||
typeof objA !== "object" || | ||
objA === null || | ||
typeof objB !== "object" || | ||
objB === null | ||
) { | ||
return false; | ||
} | ||
|
||
const keysA = Object.keys(objA); | ||
const keysB = Object.keys(objB); | ||
|
||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
|
||
// Test for A's keys different from B. | ||
for (let i = 0; i < keysA.length; i++) { | ||
const currentKey = keysA[i]; | ||
if ( | ||
!hasOwnProperty.call(objB, currentKey) || | ||
// @ts-expect-error lost refinement of `objB` | ||
!is(objA[currentKey], objB[currentKey]) | ||
) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters