Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-context-selector

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-context-selector

React useContextSelector hook in userland

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is use-context-selector?

The `use-context-selector` package is a React hook that allows for more granular context updates by enabling the selection of specific parts of the context. This can help optimize performance by reducing unnecessary re-renders in components that consume context.

What are use-context-selector's main functionalities?

Context Selection

This feature allows you to select a specific part of the context value, which can help in optimizing performance by preventing unnecessary re-renders of components that consume the context.

```jsx
import React, { createContext } from 'react';
import { useContextSelector } from 'use-context-selector';

const MyContext = createContext();

const MyComponent = () => {
  const value = useContextSelector(MyContext, (v) => v.someValue);
  return <div>{value}</div>;
};

const App = () => {
  const contextValue = { someValue: 'Hello, World!' };
  return (
    <MyContext.Provider value={contextValue}>
      <MyComponent />
    </MyContext.Provider>
  );
};

export default App;
```

Avoiding Unnecessary Re-renders

This example demonstrates how `use-context-selector` can be used to avoid unnecessary re-renders. The `MyComponent` only re-renders when `someValue` changes, not when `otherValue` changes.

```jsx
import React, { createContext, useState } from 'react';
import { useContextSelector } from 'use-context-selector';

const MyContext = createContext();

const MyComponent = () => {
  const value = useContextSelector(MyContext, (v) => v.someValue);
  console.log('MyComponent re-rendered');
  return <div>{value}</div>;
};

const App = () => {
  const [contextValue, setContextValue] = useState({ someValue: 'Hello, World!', otherValue: 42 });

  return (
    <MyContext.Provider value={contextValue}>
      <MyComponent />
      <button onClick={() => setContextValue({ ...contextValue, otherValue: contextValue.otherValue + 1 })}>
        Update Other Value
      </button>
    </MyContext.Provider>
  );
};

export default App;
```

Other packages similar to use-context-selector

Keywords

FAQs

Package last updated on 06 May 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc