πŸŽƒ Spookyberb πŸŽƒ – 12-59 Nov 3

Having a function at the parent level, is it possible to pass from the child a value parameter to the function while also passing another parameter from the parent? I want to pass a limit param from the parent, and a value param from the child
const isValid = (value, limit = 25) => value.length <= limit;

<ControlledInput validationFunction={isValid} />
const ControlledInput = ({ control, name, label, validationFunction }) => (
<Controller
...
onValueChange={(val) => {
if (validationFunction(val)) {
onChange(val);
}
}}
...
/>
);
const isValid = (value, limit = 25) => value.length <= limit;

<ControlledInput validationFunction={isValid} />
const ControlledInput = ({ control, name, label, validationFunction }) => (
<Controller
...
onValueChange={(val) => {
if (validationFunction(val)) {
onChange(val);
}
}}
...
/>
);
L
Louisβ€’532d ago
Yeah, you can change <ControlledInput validationFunction={isValid} /> to
<ControlledInput validationFunction={(value) => isValid(value, limit)} />
<ControlledInput validationFunction={(value) => isValid(value, limit)} />
Assuming limit is available in scope from the parent there meowthumbsup
S
Sniberbβ€’532d ago
damn how does that work? oooo okay thank you!
L
Louisβ€’532d ago
It’s just a callback function so you can receive the value from the child and call isValid with it and whatever is in scope in the parent No worries meowthumbsup
UU
Unknown Userβ€’532d ago