npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

conuse

v0.3.1

Published

Share Hook with Context

Downloads

6

Readme

conuse

Share Hook with Context

Get Started

import React, { useState } from 'react';
import createConuse from 'conuse';

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

// 2️⃣ Wrap your hook with the createConuse factory
const { ConuseProvider, useConuseContext } = createConuse({ counter: useCounter });

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

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

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

Installation

npm:

npm i conuse

Yarn:

yarn add conuse

API

const { ConuseProvider, useConuseContext, getContext } = createConuse(useMap[, conuse])

Conuse library exports a single factory method called createConuse which return conuse type, as follow:

Conuse {
  ConuseProvider: React.FC<any>;
  useConuseContext: (name?: string) => any;
  getContext: (name?: string) => any;
}

useMap

Type: { [name: string]: hook }

It receives custom hook map, using it to compose multiple hook. You can get one hook by passing name to useConuseContext parameter.

const { useConuseContext } = createConuse({ counter: useCounter });
const Component = () => {
  const { count } = useConuseContext('counter');
  return count;
};

conuse

Type: Conuse

Using it to compose multiple conuse.

const toggleConuse = createConuse({ toggle: useToggle });
const { useConuseContext } = createConuse({ counter: useCounter }, { toggle: toggleConuse });
const Component = () => {
  const { count } = useConuseContext('counter');
  const { toggle } = useConuseContext('toggle');
  return `${count}${toggle}`;
};

ConuseProvider

Type: React.FC<any>

Just like Context.Provider, to put the ConuseProvider at the top of your App.

<ConuseProvider>
  <App />
</ConuseProvider>

useConuseContext

Type: (name?: string) => any

The children of ConuseProvider can get certain hook by useConuseContext.

const [value, setValue] = useConuseContext(<name>)

The name parameter must be one of the keys of useMap, and you can get the returned of relevant hook which will be executed.

If you want to get all hooks, not passing name to useConuseContext. But the return of useConuseContext() is all hooks, not the returned of all hooks, you need to execute hook function to get state and setState.

getContext

Type: (name?: string) => any

The difference between getContext and useConuseContext is getContext can be used everywhere, not only in Function Component.

Inpiration

Thanks to constate and unstated-next incredible work, and learned a lot from @kentcdodds' Application State Management with React.