Skip to content

Commit

Permalink
chore: Rename parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Jan 2, 2019
1 parent 46457dd commit 376224d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -93,9 +93,9 @@ yarn add constate@next
## API ## API


### `createContainer(useValue[, createInputs])` ### `createContainer(useValue[, createMemoInputs])`


Constate exports a single method called `createContainer`. It receives two arguments: [`useValue`](#usevalue) and [`createInputs`](#createinputs) (optional). And returns `{ Context, Provider }`. Constate exports a single method called `createContainer`. It receives two arguments: [`useValue`](#usevalue) and [`createMemoInputs`](#creatememoinputs) (optional). And returns `{ Context, Provider }`.


#### `useValue` #### `useValue`


Expand Down Expand Up @@ -141,11 +141,11 @@ function Counter() {
} }
``` ```


#### `createInputs` #### `createMemoInputs`


Optionally, you can pass in a function that receives the `value` returned by `useValue` and returns an array of inputs. When any input changes, `value` gets re-evaluated, triggering a re-render on all consumers (components calling `useContext()`). Optionally, you can pass in a function that receives the `value` returned by `useValue` and returns an array of inputs. When any input changes, `value` gets re-evaluated, triggering a re-render on all consumers (components calling `useContext()`).


If `createInputs` is undefined, it'll be re-evaluated everytime `Provider` renders: If `createMemoInputs` is undefined, it'll be re-evaluated everytime `Provider` renders:


```js ```js
// re-render consumers only when value.count changes // re-render consumers only when value.count changes
Expand All @@ -168,7 +168,7 @@ import { useMemo } from "react";
const CounterContainer = createContainer(() => { const CounterContainer = createContainer(() => {
const [count, setCount] = useState(0); const [count, setCount] = useState(0);
const increment = () => setCount(count + 1); const increment = () => setCount(count + 1);
// same as passing `value => [value.count]` to `createInputs` parameter // same as passing `value => [value.count]` to `createMemoInputs` parameter
return useMemo(() => ({ count, increment }), [count]); return useMemo(() => ({ count, increment }), [count]);
}); });
``` ```
Expand Down
14 changes: 7 additions & 7 deletions src/index.tsx
Expand Up @@ -6,19 +6,19 @@ function warnNoProvider() {
console.warn("[constate] Missing Provider"); console.warn("[constate] Missing Provider");
} }


function createContainer<P, S>( function createContainer<P, V>(
useValue: (props: P) => S, useValue: (props: P) => V,
createInputs?: (state: S) => any[] createMemoInputs?: (value: V) => any[]
) { ) {
const proxy = new Proxy({}, { get: warnNoProvider, apply: warnNoProvider }); const proxy = new Proxy({}, { get: warnNoProvider, apply: warnNoProvider });


const Context = React.createContext<S>(proxy as S); const Context = React.createContext<V>(proxy as V);


const Provider = (props: { children?: React.ReactNode } & P) => { const Provider = (props: { children?: React.ReactNode } & P) => {
const state = useValue(props); const state = useValue(props);
// createInputs won't change between renders // createMemoInputs won't change between renders
const value = createInputs const value = createMemoInputs
? React.useMemo(() => state, createInputs(state)) ? React.useMemo(() => state, createMemoInputs(state))
: state; : state;
return <Context.Provider value={value}>{props.children}</Context.Provider>; return <Context.Provider value={value}>{props.children}</Context.Provider>;
}; };
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.tsx
Expand Up @@ -30,8 +30,8 @@ test("default", () => {
expect(getByText("1")).toBeDefined(); expect(getByText("1")).toBeDefined();
}); });


test("createInputs", () => { test("createMemoInputs", () => {
const Container = createContainer(useCounter, state => [state.count]); const Container = createContainer(useCounter, value => [value.count]);
const Increment = () => { const Increment = () => {
const { increment } = React.useContext(Container.Context); const { increment } = React.useContext(Container.Context);
return <button onClick={increment}>Increment</button>; return <button onClick={increment}>Increment</button>;
Expand All @@ -52,7 +52,7 @@ test("createInputs", () => {
expect(getByText("1")).toBeDefined(); expect(getByText("1")).toBeDefined();
}); });


test("empty createInputs", () => { test("empty createMemoInputs", () => {
const Container = createContainer(useCounter, () => []); const Container = createContainer(useCounter, () => []);
const Increment = () => { const Increment = () => {
const { increment } = React.useContext(Container.Context); const { increment } = React.useContext(Container.Context);
Expand Down

0 comments on commit 376224d

Please sign in to comment.