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

@uifabric/react-hooks

Package Overview
Dependencies
Maintainers
5
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uifabric/react-hooks

UI Fabric React hooks.

  • 7.0.17
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.8K
decreased by-57.91%
Maintainers
5
Weekly downloads
 
Created
Source

@uifabric/react-hooks

UI Fabric React hooks

Helpful hooks not provided by React itself.

  • useConst - Initialize and return a value that's always constant
  • useConstCallback - Like useConst but for functions
  • useId - Get a globally unique ID

useConst

function useConst<T>(initialValue: T | (() => T)): T

Hook to initialize and return a constant value. Unlike React.useMemo, this will always return the same value (and if the initializer is a function, only call it once). This is similar to setting a private member in a class constructor.

Its one parameter is the initial value, or a function to get the initial value. Similar to useState, only the first value/function passed in is respected.

If the value should ever change based on dependencies, use React.useMemo instead.

If the value itself is a function, consider using useConstCallback instead.

Example

import { useConst } from '@uifabric/react-hooks';

const MyComponent = () => {
  const value = useConst(() => {
    /* some computation that must only run once or has side effects */
  });
  const valueThatMustNeverChange = useConst(/*...*/);
  ...
};

Why not just useMemo?

According to the React docs:

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

In cases where the value must never change, the recommended workaround is to store it with useRef, but refs are more awkward to initialize and don't enforce or even communicate that the value should be immutable. An alternative workaround is const [value] = useState(initializer), but this is semantically wrong and more costly under the hood.

useConstCallback

function useConstCallback<T extends (...args: any[]) => any>(callback: T): T

Hook to ensure a callback function always has the same identity. Unlike React.useCallback, this is guaranteed to always return the same value.

Its one parameter is the callback. Similar to useState, only the first value/function passed in is respected.

If the callback should ever change based on dependencies, use React.useCallback instead.

useConstCallback(fn) has the same behavior as useConst(() => fn).

useId

function useId(prefix?: string): string

Hook to generate a unique ID (with optional prefix) in the global scope. This will return the same ID on every render.

Useful for cases in which a component may be rendered multiple times on the same page and needs to use an ID for internal purposes, such as associating a label and an input.

Example

import { useId } from '@uifabric/react-hooks';

const TextField = ({ labelText, defaultValue }) => {
  const id = useId('field');
  return (
    <div>
      <label htmlFor={id}>{labelText}</label>
      <input id={id} type="text" defaultValue={defaultValue} />
    </div>
  );
};

FAQs

Package last updated on 07 Mar 2020

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