I am trying to call a textinput child component then obtain a textinput value from the component. I am trying to pass the value back to the parent. However, I keep getting an empty value back to the parent. I can’t seem to find my error. What am I doing wrong?
Parent
export default function HdInputs(){
let [ht, setHt] = React.useState("");
let hdInput = (ht) => {
console.log("ht", ht)
setHt(ht);
}
return(
<View>
<HdParameter hdInput={hdInput} text={"Ht (cm): "} />
</View>
)
}
Child Function
export default function HdParameter(props){
let [param, setParam] = React.useState("");
let hdOutput = ()=> {
props.hdInput(param);
}
return(
<View style={AppStyles.hdParameter}>
<Text style={AppStyles.boldText}>{props.text}</Text>
<TextInput
style={[AppStyles.inputLight, { alignSelf: "center" }]}
placeholder=''
defaultValue={props.defaultValue}
placeholderTextColor={"#1b2747"}
onChangeText={setParam}
value={param}
keyboardType="numeric"
onInput={hdOutput}
/>
</View>
)
}
2
Answers
You just forgot to add a param to the
hdOutput
function insideHdParameter
.Normally you need to make the function this way:
This is a simple piece of code that show how to get some dat from a child component.
This is your code updated: