onChangeText
뷰에서 세 가지 입력을 듣고 있습니다. 그러나 세 개의 개별 onChangeText
핸들러 를 사용하는 대신 . 입력의 입력 상태 이름을 핸들러에 전달하고 그로부터 상태를 설정하고 싶습니다.
이 예제를 시도했지만 e.target.value가 undefined
. 건네 name
번째 매개 변수로하고 e
소자의 포인터 :
import React, { Component } from 'react';
import { View, StyleSheet, Alert, Image, TextInput } from 'react-native';
import { Constants } from 'expo';
export default class App extends Component {
state = {
setValue: 0,
travelValue: 0,
runValue: 0
};
_handleTextChange (e, name) {
var change = {};
change[name] = e.target.value;
this.setState(change);
}
render() {
return (
<View style={styles.container}>
<TextInput
value={this.state.setValue}
keyboardType = 'numeric'
onChangeText={this._handleTextChange.bind(this, 'setValue')}
style={{ width: 200, height: 44, padding: 8 }}
/>
<TextInput
value={this.state.travelValue}
keyboardType = 'numeric'
onChangeText={this._handleTextChange.bind(this, 'travelValue')}
style={{ width: 200, height: 44, padding: 8 }}
/>
<TextInput
value={this.state.runValue}
keyboardType = 'numeric'
onChangeText={this._handleTextChange.bind(this, 'runValue')}
style={{ width: 200, height: 44, padding: 8 }}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-between',
paddingTop: Constants.statusBarHeight,
paddingBottom: '20%',
backgroundColor: '#ecf0f1',
},
});
질문:
하나의 핸들러에서 여러 변경 이벤트를 어떻게 처리 할 수 있습니까?
잘못된 순서로 사용 했으므로 다음과 같이 사용하십시오.
_handleTextChange (name, e) { //first name then event
var change = {};
change[name] = e.target.value;
this.setState(change);
}
당신은 정의,이처럼 쓸 수있는 name
입력 요소에 속성을 그 다음 액세스 name
하여 e.target.name
이 등을 :
<TextInput
value={this.state.setValue}
name='setValue'
onChangeText={this._handleTextChange}
....
Bind
_handleTextChange function
에서 constructor
:
this._handleTextChange = this._handleTextChange.bind(this);
다음과 _handleTextChange
같이 사용하십시오 .
_handleTextChange(e){
var change = {};
change[e.target.name] = e.target.value;
this.setState(change);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다