You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@rkmodules/use-inner

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rkmodules/use-inner

internal state react hook that follows an outside state

1.0.3
latest
Source
npm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

use-inner

This is a very simple wrapper around Reacts useState. The entire implementation is this:

export const useInner = <T>(
    outer: T,
    watch?: any[]
): [T, React.Dispatch<React.SetStateAction<T>>] => {
    let [inner, setInner] = React.useState<T>(outer);
    React.useEffect(() => {
        setInner(outer);
    }, watch || [outer]);
    return [inner, setInner];
};

usage

use useInner as a local shallow copy of some prop from outside. One place I use this a lot is where a default value is given through props (which may change), but there is also an inner state.

npm install @rkmodules/use-inner
import useInner from "@rkmodules/use-inner";

examples

"lazy" input, that only updates the outside on blur

function LazyInput({ value, onChange }) {
    let [inner, setInner] = useInner(value);
    // inner now changes whenever value changes, but also on setInner

    const handleChange = (e) => {
        setInner(e.target.value);
    };
    const handleBlur = (e) => {
        onChange(inner);
    };

    return <input value={inner} onChange={handleChange} onBlur={handleBlur} />;
}

explicit watch list, useful for mapped props

function LazyArray({ value, onChange }) {
    let [inner, setInner] = useInner(value.toUpperCase(), [value]);
    // inner now changes whenever value changes, but also on setInner. In this cases also when capitalization changes

    const handleChange = (e) => {
        setInner(e.target.value);
    };
    const handleBlur = (e) => {
        onChange(inner);
    };

    return <input value={inner} onChange={handleChange} onBlur={handleBlur} />;
}

# project setup

followed https://www.twilio.com/blog/2017/06/writing-a-node-module-in-typescript.html for project setup

FAQs

Package last updated on 25 Sep 2023

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