Skip to content

Commit 7b98d85

Browse files
andys8megamaddu
andauthored
React.memo with argument (#61)
Adding another variant of memo that allows passing a function to compare previous and new props. <https://reactjs.org/docs/react-api.html#reactmemo> Solves <#60> Co-authored-by: Madeline Trotter <[email protected]>
1 parent 630baeb commit 7b98d85

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/React/Basic/Hooks.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export function reactChildrenToArray(children) {
1212
return React.Children.toArray(children);
1313
}
1414

15-
export var memo_ = React.memo;
15+
export const memo_ = React.memo;
16+
export const memoEq_ = React.memo;
1617

1718
export function useState_(tuple, initialState) {
1819
const [state, setState] = React.useState(
@@ -52,7 +53,7 @@ export function useReducer_(tuple, reducer, initialState) {
5253
return tuple(state, dispatch.$$reactBasicHooks$$cachedDispatch);
5354
}
5455

55-
export var useRef_ = React.useRef;
56+
export const useRef_ = React.useRef;
5657

5758
export function readRef_(ref) {
5859
return ref.current;
@@ -62,15 +63,15 @@ export function writeRef_(ref, a) {
6263
ref.current = a;
6364
}
6465

65-
export var useContext_ = React.useContext;
66+
export const useContext_ = React.useContext;
6667
export { useEqCache as useEqCache_ };
6768

6869
export function useMemo_(eq, deps, computeA) {
6970
const memoizedKey = useEqCache(eq, deps);
7071
return React.useMemo(computeA, [memoizedKey]);
7172
}
7273

73-
export var useDebugValue_ = React.useDebugValue;
74+
export const useDebugValue_ = React.useDebugValue;
7475

7576
export function unsafeSetDisplayName(displayName, component) {
7677
component.displayName = displayName;

src/React/Basic/Hooks.purs

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module React.Basic.Hooks
88
, reactChildrenToArray
99
, reactChildrenFromArray
1010
, memo
11+
, memo'
1112
, useState
1213
, useState'
1314
, UseState
@@ -166,6 +167,16 @@ memo ::
166167
Effect (ReactComponent props)
167168
memo = flip Prelude.bind (runEffectFn1 memo_)
168169

170+
-- | Similar to `memo` but takes a function to compare previous and new props
171+
memo' ::
172+
forall props.
173+
(props -> props -> Boolean) ->
174+
Effect (ReactComponent props) ->
175+
Effect (ReactComponent props)
176+
memo' arePropsEqual comp = Prelude.do
177+
c <- comp
178+
runEffectFn2 memoEq_ c (mkFn2 arePropsEqual)
179+
169180
useState ::
170181
forall state.
171182
state ->
@@ -359,6 +370,13 @@ foreign import memo_ ::
359370
(ReactComponent props)
360371
(ReactComponent props)
361372

373+
foreign import memoEq_ ::
374+
forall props.
375+
EffectFn2
376+
(ReactComponent props)
377+
(Fn2 props props Boolean)
378+
(ReactComponent props)
379+
362380
foreign import unsafeSetDisplayName ::
363381
forall props.
364382
EffectFn2 String (ReactComponent props) (ReactComponent props)

0 commit comments

Comments
 (0)