コンポーネントの外にある関数からReact-nativeコンポーネントのテキスト入力にアクセスしようとしています。clearForm()関数の参照を介してTextInputをクリアしたい。
MyComponent.js
import React, { Component } from 'react';
import {
View,
TextInput,
} from 'react-native';
class myComponent extends Component {
render() {
return (
<View style={styles.container}>
<TextInput
ref= {'email'}
style={styles.input}
placeholder="Email"
/>
</View>
);
}
}
Actions.js
function clearForm(data){
for(var input_name in data){
/////???
//this.refs[input_name].setNativeProps({text: ''});
}
}
時間がかかって申し訳ありませんが、これでうまくいくはずです。
// myComponent.js
class myComponent extends Component {
constructor(props) {
this.state = {
emailText: ''
}
}
clearField() {
this.setState({
emailText: ''
})
}
render() {
return (
<View style={styles.container}>
<TextInput
ref= {'email'}
style={styles.input}
placeholder="Email"
value={this.state.emailText}
/>
</View>
);
}
}
// Actions.js
function clearForm(data){
for(var input_name in data){
this.refs[input_name].clearField();
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加