Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active November 10, 2017 17:19
Show Gist options
  • Save abiodun0/a6b2c135f83946b7e2dd94832f2e9ee5 to your computer and use it in GitHub Desktop.
Save abiodun0/a6b2c135f83946b7e2dd94832f2e9ee5 to your computer and use it in GitHub Desktop.
Reselect Done with Ramda function
const createSelector = (...fns) => R.memoize(R.converge(R.last(fns), R.init(fns)))
// test case
const selector = createSelector(
state => state.a,
state => state.b,
(a, b) => console.log('Called!') || ({
c: a * 2,
d: b * 3
})
)
const test = () => {
console.log(R.equals(selector({ a: 1, b: 2 }), { c: 2, d: 6 })) // called once
console.log(R.equals(selector({ a: 2, b: 3 }), { c: 4, d: 9 })) // called once
console.log(R.equals(selector({ a: 1, b: 2 }), { c: 2, d: 6 }))
console.log(R.equals(selector({ a: 2, b: 3 }), { c: 4, d: 9 }))
console.log(R.equals(selector({ a: 1, b: 2 }), { c: 2, d: 6 }))
console.log(R.equals(selector({ a: 2, b: 3 }), { c: 4, d: 9 }))
}
test()
import React from "react";
import { render } from "react-dom";
import R from "ramda";
const createSelector = (...fns) =>
R.memoize(R.converge(R.last(fns), R.init(fns)));
const selector = createSelector(
props => props.a,
props => props.b,
(a, b) =>
console.log("Called!") || {
calculatedA: a * 2,
calculatedB: b * 3
}
);
const LowLevelComponent = ({ calculatedA, calculatedB }) =>
<div>
A: {calculatedA}, B: {calculatedB}
</div>;
const EnhancedLowLevelComponent = R.compose(LowLevelComponent, selector);
const App = () => {
return (
<div>
<EnhancedLowLevelComponent a={1} b={2} />
</div>
);
};
render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment