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

react-native-use-on-change

v1.0.18

Published

Easy to use hook.

Downloads

28

Readme

react-native-use-on-change

Hook provides easy functionality to validate and handle forms. Don't duplicate your code! Just use this hook to build all your validation.

Installation

expo: expo install react-native-use-on-change
npm: npm i react-native-use-on-change
yarn: yarn add react-native-use-on-change

Basic usage

Input.jsx

import {TextInput, View} from "react-native";
import React from "react";


export default function Input({name, error, onChange, value}) {
    let errorToShow = error?.[name];

    const handleChange = text => onChange({name, value: text});

    return(
        <View
            style={{
                height: 60,
                marginVertical: 5,
                backgroundColor: 'gray',
                width: '100%'
            }}
        >
            <TextInput style={{flex: 1,}} value={value?.[name]} onChangeText={handleChange}/>
        </View>
    )
}
import useOnChange from "react-native-use-on-change";

export default function YourComponent() {
// You can use many things from hook and define names you want. 
// 1. data is basicaly your data Object with key:value for each field.
// 2. onChange function which should be passed to inputs to handle their state.
   const [data, onChange] = useOnChange({
        initialState: {
            field1: '',
            field2: '',
        },
    });

   return(
       <View
            style={{
                flex: 0.7
            }}
        >
            <View
                style={{
                    flex: 0.5,
                    justifyContent: 'center',
                    overflow: 'hidden'
                }}
            >
                <Input name={'field1'} value={data} onChange={onChange}/>
                <Input name={'field2'} value={data} onChange={onChange}/>                    
            </View>               
        </View>
   )
}

// more about customizing below

Advanced usage

In case you want to use this hook you should prepare any form components to use onChange, value, errors, name. Remember they required to use this hook!

For example: Input.jsx

import {TextInput, View} from "react-native";
import React from "react";


export default function Input({name, error, onChange, value}) {
    let errorToShow = error?.[name];

    const handleChange = text => onChange({name, value: text});

    return(
        <View
            style={{
                height: 60,
                marginVertical: 5,
                backgroundColor: 'gray',
                width: '100%'
            }}
        >
            <TextInput style={{flex: 1,}} value={value?.[name]} onChangeText={handleChange}/>
        </View>
    )
}

Then we use our prepared input somewhere in our app with useOnChange hook. And that's all.


// Custom validator
const customValidator = (paramToCheck, message) => (value) => value === paramToCheck ? '' : message;

export default function Container() {
   const [otherValue, setOtherValue] = useState(false);
   // You can use many things from hook and define names you want. 
   // 1. data is basicaly your data Object with key:value for each field.
   // 2. onChange function which should be passed to inputs to handle their state.
   // 3. errors created by validation functions.
   // 4. canSave property which shows to us when all fields is filled and passed validation.
   // 5. requestFunction is function you want to use to save validated data.
   // 6. loading based or requestFunction state you can show users when you process their data.
    const [data, onChange, errors, canSave, save, saving] = useOnChange({
        // Initial state
        initialState: {
            field1: '',
            field2: '',
        },
        // Validators with validate functions for each field
        validators: {
            field1: [
                isRequired('This field is required'),
                minLength(5, 'Min value is 5'),
                maxLength(255, 'Max is 255'),
            ],
            field2: [
                isRequired('This field is required'),
                minLength(5, 'Min value is 5'),         
                maxLength(255, 'Max is 255'),
                // We added our custom validator
                customValidator('Sample string', 'Value should be sample string')
            ]
        },
        config: {
            // Method for saving
            fetchMethod: async (data) => {
                // Save data somewhere
            }
        },
        canSaveConfig: {
            // If data should not be equal to save
            cantSaveUnchanged: true,
            // Custom validation checker
            validationFunction: (data, errors, state) => {
                // Some of your custom validation code it should return boolean
            }
        }
        // Deeps to reset hook
    }, [otherValue]);

    return (
        <View
            style={{
                flex: 1,
                justifyContent: 'center',
                alignItems: 'center'
            }}
        >
            <Input name={'field1'} value={data} onChange={onChange} error={errors}/>
            <Input name={'field2'} value={data} onChange={onChange} error={errors}/>
            <TouchableOpacity
                disabled={!canSave}
                style={[{
                    flexDirection: 'row',
                    height: 60,
                    width: '100%',
                    backgroundColor: 'green',
                    justifyContent: 'center',
                    alignItems: 'center',
                }, !canSave && {
                    backgroundColor: 'red',
                }]}
                onPress={save}
            >
                <Text>
                    Save
                </Text>
            </TouchableOpacity>
            <TouchableOpacity
                style={[{
                    flexDirection: 'row',
                    height: 60,
                    width: '100%',
                    backgroundColor: 'blue',
                    justifyContent: 'center',
                    alignItems: 'center',
                }]}
                onPress={() => setOtherValue(!otherValue)}
            >
                <Text>
                    Reset hook
                </Text>
            </TouchableOpacity>
        </View>
    )
}
// more about customizing below

Validators

| Name | Params | Params | | -------------------------- | --------- | --------- | | minLength(8, 'Your min message') | (number, string) | Number of min characters and message to show when validation fails | | maxLength(8, 'Your max message') | (number, string) | Number of max characters and message to show when validation fails | | isRequired('Your message') | (string) | Required field validation | | isValueEqual('field_name', 'Your message') | (string, string) | Take field name to check if value equal to this field value | | isPhoneValid('Your message') | (string) | Validate phone number field | | isEmailValid('Your message') | (string) | Validate your email field | | isValidHex('Your message') | (string) | Hex validation |