Giter Club home page Giter Club logo

recoil-context's Introduction

recoil-context: a distributed context on top of Recoil

Installation

npm install --save recoil-context recoil
// or
yarn add recoil-context recoil

Example

// WizardContext.ts

import { createContext } from "recoil-context";

const { RecoilContext, useContextValue, useSetContextValue } = createContext({
  paymentMethod: "card" as "card" | "paypal",
  step: 1,
});

export { useContextValue, useSetContextValue };

export default RecoilContext;
// Wizard.ts

import WizardContext, { useContextValue, useSetContextValue } from "./Wizard";

const Tab = ({ children, id }) => {
  const setStep = useSetContextValue("step");
  return <button onClick={() => setStep(id)}>{children}</button>;
};

const Tabs = () => {
  const method = useContextValue("paymentMethod");
  const step = useContextValue("step");
  
  return (
    <div>
      <Tab id={1} completed={step > 1}>Payment details</Tab>
      <Tab id={2} completed={step > 2}>
        {method === "card" ? "Card details" : "PayPal authorization"}
      </Tab>
      <Tab id={3} completed={step > 3}>Confirmation</Tab>
    </div>
  )
};

const Panels = () => {
  const method = useContextValue("paymentMethod");
  const step = useContextValue("step");  
  if (step === 1) { /* … */ }
  if (step === 2 && method === "card") { /* … */ }
  if (step === 2 && method === "paypal") { /* … */ }
  if (step === 3) { /* … */ }
}

const Wizard = () => {
  return (
    <WizardContext initialValues={{ paymentMethod: "paypal" }}>
      <Tabs />
      <Panels />
    </WizardContext>
  );
}

API

recoil-context exports only one function:

createContext<TValues>(defaultValues: TValues): { RecoilContext, useContextValue, useSetContextValue }.

It accepts an object with default values and creates a strictly typed context and pair of hooks for getting and setting values:

  • RecoilContext is a container for your state and provides values for hooks. Default values can be fully or partially overridden with initialValues prop;
  • useContextValue<T extends keyof TValues>(name: T): TValues[T] returns current value from the context;
  • useSetContextValue<T extends keyof TValues>(name: T): Dispatch<SetStateAction<TValues[T]>> returns a value setter for specified field.

License

MIT

recoil-context's People

Contributors

anber avatar dependabot[bot] avatar

Stargazers

Andrejs Agejevs avatar Michael Demarais avatar

Watchers

 avatar James Cloos avatar Michael Demarais avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.