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() {
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>
)
}
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.
const customValidator = (paramToCheck, message) => (value) => value === paramToCheck ? '' : message;
export default function Container() {
const [otherValue, setOtherValue] = useState(false);
const [data, onChange, errors, canSave, save, saving] = useOnChange({
initialState: {
field1: '',
field2: '',
field3: '',
},
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'),
customValidator('Sample string', 'Value should be sample string')
],
},
config: {
fetchMethod: async (data) => {
}
},
canSaveConfig: {
cantSaveUnchanged: true,
validationFunction: (data, errors, state) => {
}
}
}, [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>
)
}
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 |