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

use-binding

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-binding - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

11

dist/index.d.ts

@@ -0,2 +1,5 @@

import { Dispatch } from 'react';
declare type SetBindingStateAction<S, I> = S | ((prevState: I) => S);
declare type ChangeHandler<T> = (newValue: T) => void;
declare type Setter<T, I = T> = Dispatch<SetBindingStateAction<T, I>>;
/**

@@ -12,3 +15,3 @@ * Return a [value, setValue] pair for a binding.

*/
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null, fallbackValue: T): [T, ChangeHandler<T>];
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null, fallbackValue: T): [T, Setter<T>];
/**

@@ -21,3 +24,3 @@ * Return a [value, setValue] pair for a binding.

*/
export declare function useBinding<T>(defaultValue: T, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T, ChangeHandler<T>];
export declare function useBinding<T>(defaultValue: T, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>];
/**

@@ -30,3 +33,3 @@ * Return a [value, setValue] pair for a binding.

*/
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T, onChange: ChangeHandler<T> | undefined | null): [T, ChangeHandler<T>];
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T, onChange: ChangeHandler<T> | undefined | null): [T, Setter<T>];
/**

@@ -39,3 +42,3 @@ * Return a [value, setValue] pair for a binding.

*/
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T | null, ChangeHandler<T>];
export declare function useBinding<T>(defaultValue: T | undefined | null, value: T | undefined | null, onChange: ChangeHandler<T> | undefined | null): [T | null, Setter<T, T | null>];
export {};

@@ -1,2 +0,2 @@

import { useState } from 'react';
import { useState, useRef, useCallback } from 'react';

@@ -18,12 +18,15 @@ function useBinding(defaultValue, value, onChange, fallbackValue) {

var changeHandler = onChange || noop;
if (hasControlledValue) {
return [inputValue, changeHandler];
}
return [
uncontrolledValue,
function (newValue) {
changeHandler(newValue);
setUncontrolledValue(newValue);
},
];
var currentValueRef = useRef(null);
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue;
var setter = useCallback(function (newValue) {
var evaluatedNewValue = typeof newValue === 'function'
? newValue(currentValueRef.current)
: newValue;
currentValueRef.current = evaluatedNewValue;
changeHandler(evaluatedNewValue);
if (!hasControlledValue) {
setUncontrolledValue(evaluatedNewValue);
}
}, [currentValueRef, changeHandler, hasControlledValue]);
return [currentValueRef.current, setter];
}

@@ -30,0 +33,0 @@ function noop(_) {

@@ -22,12 +22,15 @@ 'use strict';

var changeHandler = onChange || noop;
if (hasControlledValue) {
return [inputValue, changeHandler];
}
return [
uncontrolledValue,
function (newValue) {
changeHandler(newValue);
setUncontrolledValue(newValue);
},
];
var currentValueRef = react.useRef(null);
currentValueRef.current = hasControlledValue ? inputValue : uncontrolledValue;
var setter = react.useCallback(function (newValue) {
var evaluatedNewValue = typeof newValue === 'function'
? newValue(currentValueRef.current)
: newValue;
currentValueRef.current = evaluatedNewValue;
changeHandler(evaluatedNewValue);
if (!hasControlledValue) {
setUncontrolledValue(evaluatedNewValue);
}
}, [currentValueRef, changeHandler, hasControlledValue]);
return [currentValueRef.current, setter];
}

@@ -34,0 +37,0 @@ function noop(_) {

{
"name": "use-binding",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "peerDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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