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

smart-context-hooks

v0.1.4

Published

Leverage React Context to optimize rendering performance by minimizing unnecessary component re-renders when state changes.

Downloads

4

Readme

Smart Context Hooks

Boost performance in your React applications with optimized context management.

This library provides hooks that enhance the efficiency of React Context by minimizing unnecessary re-renders caused by state changes. It's designed to be lightweight and non-intrusive, working seamlessly with your existing React Context setup.

Key Features:

  • Reduced re-renders: Optimize performance by selectively re-rendering components only when relevant data changes.
  • Seamless integration: Works seamlessly with your existing React Context implementation, requiring no major code changes.
  • TypeScript support: Provides full type safety for context values, enhancing type checking and code maintainability.
  • JavaScript compatibility: Also functions well in pure JavaScript projects.

Not a replacement, but a valuable enhancement. This library is not intended to replace React Context or Redux, but rather to complement your existing state management strategy by improving its performance.

Live Demo:

Check out the live demo app using smart context: Smart Context Demo

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save smart-context-hooks

or using yarn

yarn add smart-context-hooks

Usage

Below are examples of how to use the smart-context-hooks package in both a TypeScript and JavaScript application.

Creating smart context

  1. Define your context interface: (TypeScript only) Create an interface to define the type of your context state. This ensures type safety and improves code readability.
// TypeScript

interface AppContext extends SmartContext {
    setCount: React.Dispatch<React.SetStateAction<number>>;
    // Add other state mutating functions here
}
  1. Create a context provider: Use React.createContext to create a context with your defined interface as the type.
// TypeScript

export const appContext = React.createContext<AppContext>({} as AppContext);

or using JavaScript

// JavaScript

export const appContext = React.createContext({});
  1. Create selectors: Create selector functions that return specific portions of the state.
// TypeScript

interface AppState {
    count: number;
}

export const selectCount = (state: AppState) => state.count;

or using JavaScript

// JavaScript

export const selectCount = (state) => state.count;
  1. Create Context Provider Value With useSmartContext: Initialize a context provider as you normally would except use the useSmartContext hook to return a value object to set as the provider's context value. The useSmartContext hook accepts the context's state as the first argument and the smart context object as the second argument. See more about useSmartContext below in the API reference.
// TypeScript

export const AppContextProvider = ({ children }: { children: ReactNode }) => {
    const [count, setCount] = useState(0);

    const value = useSmartContext({ count }, { setCount });

    return <appContext.Provider value={value}>{children}</appContext.Provider>;
};

or using JavaScript

// JavaScript

export const AppContextProvider = ({ children }) => {
    const [count, setCount] = useState(0);

    const value = useSmartContext({ count }, { setCount });

    return <appContext.Provider value={value}>{children}</appContext.Provider>;
};

Complete Example

// TypeScript

import { ReactNode, createContext, useState } from 'react';
import { type SmartContext, useSmartContext } from 'smart-context';

interface AppContext extends SmartContext {
    setCount: React.Dispatch<React.SetStateAction<number>>;
}

export const appContext = createContext<AppContext>({} as AppContext);

interface AppState {
    count: number;
}

export const selectCount = (state: AppState) => state.count;

export const AppContextProvider = ({ children }: { children: ReactNode }) => {
    const [count, setCount] = useState(0);

    const value = useSmartContext({ count }, { setCount });

    return <appContext.Provider value={value}>{children}</appContext.Provider>;
};

or using JavaScript

// JavaScript

import { ReactNode, createContext, useState } from 'react';
import { SmartContext, useSmartContext } from 'smart-context';

export const appContext = createContext({});

export const selectCount = (state) => state.count;

export const AppContextProvider = ({ children }) => {
    const [count, setCount] = useState(0);

    const value = useSmartContext({ count }, { setCount });

    return <appContext.Provider value={value}>{children}</appContext.Provider>;
};

Accessing State

Use the useContextSelector hook to access state data in your components. This has the benefit of only updating your component when the portion of state returned by the hook actually updates. See more about useContextSelector below in the API reference.

// TypeScript or JavaScript

import { useContextSelector } from 'smart-context';
import { appContext, selectCount } from '../../context/AppContext';

export const MyComponent = () => {
    // count will be returned with the type 'number' to match
    // the return type of the selectCount selector.
    const count = useContextSelector(appContext, selectCount);

    return <div>Count: {count}</div>;
};

Setting State

Use the useContextSetters hook to get any of the state mutating functions defined on the given context. See more about useContextSetters below in the API reference.

// Typescript or JavaScript

import { useContextSetters } from 'smart-context';
import { appContext } from '../../context/AppContext';

export const ExampleComponent = () => {
    // setCount will be returned with the exact type that was defined
    // in the context's interface that extended 'SmartContext'.
    const { setCount } = useContextSetters(appContext);

    return (
        <button onClick={() => setCount((previous) => previous + 1)}>
            Increment
        </button>
    );
};

Convert Existing React Context To Use Smart Context

Details coming soon

API Reference

useSmartContext

Description

This hook creates a smart context value that provides efficient state management and optimization features within a React application. It enables precise control over state updates and re-renders within the context, promoting performance and flexibility.

Arguments

|Name|Type|Description| |-|-|-| |state|object|An object containing the state values that can be accessed using selectors.| |setters|object|An object containing functions that update specific parts of the state object. These functions serve as the primary mechanism for state mutations within the context.|

Returns

A SmartContext object to be passed to the value prop of your context provider.


useContextSelector

Description

This hook efficiently retrieves a portion of state from a smart context using a selector function. It optimizes component re-renders by only triggering them when the selected value genuinely changes, preventing unnecessary updates and enhancing performance.

Arguments

|Name|Type|Description| |-|-|-| |context|T extends SmartContext|A React context instance created using useSmartContext| |selector|(state: any) => K|A function that takes the context state as input and returns the specific value or slice of state you want to access.|

Returns

The selected state value from the context, as determined by the provided selector function.


useContextSetters

Description

This hook provides access to the state mutating functions defined on a smart context created with the useSmartContext hook. These functions allow you to update the context state in a way that optimizes re-renders.

Arguments

|Name|Type|Description| |-|-|-| |context|T extends SmartContext|A React context instance created using useSmartContext|

Returns

An object containing all the state mutating functions defined on the provided context.