道具。onChangeText(1, v)不工作

时间:2021-11-10 19:45:42

Please how do i get the values in the text input i need to add them and divide it by the number of semesters

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

Here is my code

这是我的代码

 <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 个解决方案

#1


0  

You can do this

你可以这样做

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>
  )
}

#1


0  

You can do this

你可以这样做

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>
  )
}