Last active
September 1, 2017 20:45
-
-
Save rupeshtiwari/8481718957a8bf1e03f484aaea08d524 to your computer and use it in GitHub Desktop.
Reselect Selector example for the form control. Validating a Form which has many fields using Reselect Selectors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
createSelector | |
} = Reselect; | |
const form = { | |
firstName: 'Rakesh', | |
lastName: 'Tiwari', | |
age: 12, | |
height: 157 | |
}; | |
const getFirstName = form => form.firstName; | |
const getlastName = form => form.lastName; | |
const getAge = form => form.age; | |
const getHeight = form => form.height; | |
const isNotEmptyString = (s) => s !== ''; | |
const isValidNumber = (n) => n > 0; | |
const isFirstNameValid = createSelector( | |
getFirstName, isNotEmptyString | |
); | |
const isLastNameValid = createSelector( | |
getlastName, isNotEmptyString | |
); | |
const isAgeValid = createSelector( | |
getAge, isValidNumber | |
); | |
const isHeightValid = createSelector( | |
getHeight, isValidNumber | |
); | |
const isFormValid = createSelector( | |
isFirstNameValid, | |
isLastNameValid, | |
isAgeValid, | |
isHeightValid, (firstNameValid, | |
lastNameValid, | |
ageValid, | |
heightValid) => | |
firstNameValid && lastNameValid && ageValid && heightValid | |
); | |
console.log('form is valid', isFormValid(form)); | |
const form1 = Object.assign({},form, {age:0}); | |
console.log('form is valid', isFormValid(form1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment