Socket
Socket
Sign inDemoInstall

constate

Package Overview
Dependencies
0
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    constate

Yet another React state management library that lets you work with local state and scale up to global state with ease


Version published
Maintainers
1
Install size
25.4 kB
Created

Readme

Source

constate logo

Constate

NPM version NPM downloads Gzip size Dependencies Build Status Coverage Status

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


🕹 CodeSandbox demos 🕹
CounterI18nThemingTypeScriptWizard Form

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

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

// 2️⃣ Create container
const MainCounter = createContainer(useCounter);

function Button() {
  // 3️⃣ Use container context instead of custom hook
  // const { increment } = useCounter();
  const { increment } = useContext(MainCounter.Context);
  return <button onClick={increment}>+</button>;
}

function Count() {
  // 4️⃣ Use container context in other components
  // const { count } = useCounter();
  const { count } = useContext(MainCounter.Context);
  return <span>{count}</span>;
}

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

Installation

npm:

npm i constate@next

Yarn:

yarn add constate@next

You'll need to install react@next and react-dom@next

Constate v1 is currently in early alpha. If you're looking for v0, see v0 docs or read the migration guide.

API

createContainer(useValue[, createInputs])

Constate exports a single method called createContainer. It receives two arguments: useValue and createInputs (optional). And returns { Context, Provider }.

useValue

It's a custom hook that returns the Context value:

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

const MainCounter = createContainer(() => {
  const [count] = useState(0);
  return count;
});

console.log(MainCounter); // { Context, Provider }

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

const MainCounter = createContainer(({ initialCount = 0 }) => {
  const [count] = useState(initialCount);
  return count;
});

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

The value returned in useValue will be accessible when using useContext(MainCounter):

import React, { useContext } from "react";

function Counter() {
  const count = useContext(MainCounter);
  console.log(count); // 10
}
createInputs

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:

// re-render consumers only when value.count changes
const MainCounter = createContainer(useCoutner, value => [value.count]);

function useCounter() {
  const [count, setCount] = useState(0);
  const increment = () => setCount(count + 1);
  return { count, increment };
}

This works similarly to the inputs parameter in React.useEffect and other React built-in hooks. In fact, Constate passes it to React.useMemo inputs internally.

You can also achieve the same behavior within the custom hook. This is an equivalent implementation:

import { useMemo } from "react";

const MainCounter = createContainer(() => {
  const [count, setCount] = useState(0);
  const increment = () => setCount(count + 1);
  // same as passing `value => [value.count]` to `createInputs` parameter
  return useMemo(() => ({ count, increment }), [count]);
});

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:start to run examples.

License

MIT © Diego Haz

Keywords

FAQs

Last updated on 14 Dec 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc