props.onChangeText(1,v)不起作用

时间:2018-03-21 00:59:11

标签: javascript android reactjs react-native

请问如何获取文本输入中的值我需要添加它们并将其除以学期数

这是我的代码

 <View style={styles.levelCard}>
    <Text style={styles.levelTitle}> {props.level} LEVEL </Text>
    <View style={styles.semesterRow}>
      <Text style={styles.semester}> 1st Semester </Text>
      <TextInput style={styles.semesterInput}
        maxLength={4}
        keyboardType="numeric"
        placeholder="1st Semester's GPA"
        onChangeText={(v) => props.onChangeText(1, v)} />
    </View>
    <View style={styles.semesterRow}>
      <Text style={styles.semester}> 2nd Semester </Text>
      <TextInput style={styles.semesterInput}
        keyboardType="numeric"
        maxLength={4}
        placeholder="2nd Semester's GPA"
        onChangeText={(v) => props.onChangeText(2, v)} />
    </View>
  </View>

1 个答案:

答案 0 :(得分:0)

你可以这样做

constructor() {
  this.state = {
    semester1: 0,
    semester2: 0,
    result: 0
  }
}

onChangeText(semester, score) {
  if (semester === 1) {
    this.setState({ semester1: score })
  } else {
    this.setState({ semester2: score })
  }
  const { semester1, semester2 } = this.state;
  this.setState({
    result: (semester1 + semester2) / 2
  })
}

render() {
  return(
    <View>
      <ChildComponent onChangeText={this.onChangeText.bind(this)} />
    </View>
  )
}