A library for creating “contextors”, which efficiently select and combine values from React contexts.
// Create a contextor from one or more contexts
const Contextor1 =
createContextor(
[MyContext],
(myContextValue) => myContextValue.foo
);
// Contextors can depend on other contextors
const Contextor2 =
createContextor(
[Contextor1, MyOtherContext],
(val1, myOtherVal) => val1 + myOtherVal.bar
);
// We then read the contextors' values with the `useContextor` hook:
const Component = () => {
const value1 = useContextor(Contextor1);
const value2 = useContextor(Contextor2);
return <div>{`${value1} / ${value2}`}</div>
}
// Contextors can accept a parameter ...
const UserContextor =
createContextor(
[AllUsersContext],
(allUsers, userId) => allUsers[userId]);
// ... which is provided to `useContextor`:
const UserDisplay = ({ userId }) => {
const userObject = useContextor(UserContextor, userId);
return <div>{userObject.name}: {userObject.status}</div>
}
Contextors are implemented in TypeScript, and enforce type safety on contextor creation and usage.