3.3.2 • Published 2 years ago

constate v3.3.2

Weekly downloads
58,889
License
MIT
Repository
github
Last release
2 years ago

Constate

Write local state using React Hooks and lift it up to React Context only when needed with minimum effort.

Basic example

import React, { useState } from "react";
import constate from "constate";

// 1️⃣ Create a custom hook as usual
function useCounter() {
  const [count, setCount] = useState(0);
  const increment = () => setCount(prevCount => prevCount + 1);
  return { count, increment };
}

// 2️⃣ Wrap your hook with the constate factory
const [CounterProvider, useCounterContext] = constate(useCounter);

function Button() {
  // 3️⃣ Use context instead of custom hook
  const { increment } = useCounterContext();
  return <button onClick={increment}>+</button>;
}

function Count() {
  // 4️⃣ Use context in other components
  const { count } = useCounterContext();
  return <span>{count}</span>;
}

function App() {
  // 5️⃣ Wrap your components with Provider
  return (
    <CounterProvider>
      <Count />
      <Button />
    </CounterProvider>
  );
}

Learn more

Advanced example

import React, { useState, useCallback } from "react";
import constate from "constate";

// 1️⃣ Create a custom hook that receives props
function useCounter({ initialCount = 0 }) {
  const [count, setCount] = useState(initialCount);
  // 2️⃣ Wrap your updaters with useCallback or use dispatch from useReducer
  const increment = useCallback(() => setCount(prev => prev + 1), []);
  return { count, increment };
}

// 3️⃣ Wrap your hook with the constate factory splitting the values
const [CounterProvider, useCount, useIncrement] = constate(
  useCounter,
  value => value.count, // becomes useCount
  value => value.increment // becomes useIncrement
);

function Button() {
  // 4️⃣ Use the updater context that will never trigger a re-render
  const increment = useIncrement();
  return <button onClick={increment}>+</button>;
}

function Count() {
  // 5️⃣ Use the state context in other components
  const count = useCount();
  return <span>{count}</span>;
}

function App() {
  // 6️⃣ Wrap your components with Provider passing props to your hook
  return (
    <CounterProvider initialCount={10}>
      <Count />
      <Button />
    </CounterProvider>
  );
}

Learn more

Installation

npm:

npm i constate

Yarn:

yarn add constate

API

constate(useValue[, ...selectors])

Constate exports a single factory method. As parameters, it receives useValue and optional selector functions. It returns a tuple of [Provider, ...hooks].

useValue

It's any custom hook:

import { useState } from "react";
import constate from "constate";

const [CountProvider, useCountContext] = constate(() => {
  const [count] = useState(0);
  return count;
});

You can receive props in the custom hook function. They will be populated with <Provider />:

const [CountProvider, useCountContext] = constate(({ initialCount = 0 }) => {
  const [count] = useState(initialCount);
  return count;
});

function App() {
  return (
    <CountProvider initialCount={10}>
      ...
    </CountProvider>
  );
}

The API of the containerized hook returns the same value(s) as the original, as long as it is a descendant of the Provider:

function Count() {
  const count = useCountContext();
  console.log(count); // 10
}

selectors

Optionally, you can pass in one or more functions to split the custom hook value into multiple React Contexts. This is useful so you can avoid unnecessary re-renders on components that only depend on a part of the state.

A selector function receives the value returned by useValue and returns the value that will be held by that particular Context.

import React, { useState, useCallback } from "react";
import constate from "constate";

function useCounter() {
  const [count, setCount] = useState(0);
  // increment's reference identity will never change
  const increment = useCallback(() => setCount(prev => prev + 1), []);
  return { count, increment };
}

const [Provider, useCount, useIncrement] = constate(
  useCounter,
  value => value.count, // becomes useCount
  value => value.increment // becomes useIncrement
);

function Button() {
  // since increment never changes, this will never trigger a re-render
  const increment = useIncrement();
  return <button onClick={increment}>+</button>;
}

function Count() {
  const count = useCount();
  return <span>{count}</span>;
}

Contributing

If you find a bug, please create an issue providing instructions to reproduce it. It's always very appreciable if you find the time to fix it. In this case, please submit a PR.

If you're a beginner, it'll be a pleasure to help you contribute. You can start by reading the beginner's guide to contributing to a GitHub project.

When working on this codebase, please use yarn. Run yarn examples to run examples.

License

MIT © Diego Haz

@elliemae/ds-shuttle@axondev/auth-coresykmeldinger-testknotty-nodes-library@navikt/familie-hooks@getcircuit/componentsonecam-sharedbextikkz-bextsykmeldinger-test3raylo-apollo@everything-registry/sub-chunk-1374iarpc-componentslz-hookslocale-resource-editoreloisa-dsfn-dsfn-dsmflow-design-reactflow-diagrampaintboxkelp-mobile-walletpooppyqueying-jblz-biz-componentsreact-doc-titlereact-progressive-formrejurereasthemed-styles-reacttelemedicine-c@kyma-project/asyncapi-react@kyma-project/dc-markdown-render-engine@kyma-project/documentation-component@kyma-project/odata-react@kunk/react@plasmicpkgs/plasmic-tabs@navikt/familie-http@elliemae/ds-legacy-shuttle@eisgs/attach@eisgs/datepicker@eisgs/table@fazor/fazor@raisenow/tamaro-core@orthly/sidekick@paintbox/native@mediatr/react@hamgom95/native-rpn@real-system/state@real-system/state-libraryvzhangdev_fta_clientview_investment_overview@karimsa/sidekick@knottynodes/flow@knottynodes/react-library@jmoxey/reakit@1023-ventures/darri-auth@1023-ventures/darri-core@1023-ventures/darri-mui@1023-ventures/darri-pp-permissions@1023-ventures/lyra-auth@1023-ventures/lyra-core@1023-ventures/lyra-mui@1023-ventures/lyra-pp-permissions@1023-ventures/ursa-core@1023-ventures/vega-core@superficial-ui/components@superficial-ui/core@vzhangdev/fta_cash_management@zalastax/nolb-const@wonderflow/react-components@sitearcade/react-use-flags@trustpad/admin@trustpad/launchpad@thirty-one/primitives@auxo.dev/rp@auxo.lib/rp@zippie/runtime@contentful/field-editor-markdown@contentful/field-editor-reference@contentful/field-editor-rich-text@contentful/experience-builder-storybook-addon@contentful/experiences-storybook-addonapp-tutorialclientview_bankingconstate-clusterchaos-componentsdata-hub-componentsdolphindb-client-shared
3.3.1

2 years ago

3.3.2

2 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

1.0.0-alpha.7

5 years ago

1.0.0-alpha.6

5 years ago

1.0.0-alpha.5

5 years ago

1.0.0-alpha.4

5 years ago

1.0.0-alpha.3

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago

0.9.0

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago