0
我試圖給我的自定義組件選項使用ref,但我不知道如何去做,最好的方法是做什麼?React/React-Native Refs [修正]
<View style={ styles.wrapItems }>
<TouchableOpacity onPress={() => this.emailInput.onError() }>
<Text>Show Error</Text></TouchableOpacity>
<InputField ref={(ref) => this.emailInput = ref } alignItems={'center'} placeholder="Your Email" />
<InputField ref={(ref) => this.passwordInput = ref } secureTextEntry={true} placeholder="Your Password" />
</View>
在我的組件
export default class InputField extends Component {
constructor(props) {
super(props);
}
-static- onError() {
alert('On Error');
}
return (
<View style={ styles.inputWr }>
<TextInput
style={ [styles.input, textDir, textColor] }
onChangeText={this.onChangeText}
keyboardType={keyboardType}
underlineColorAndroid={'rgba(0,0,0,0)'}
onFocus={this.onFloatLabel}
secureTextEntry={secureTextEntry}
value={this.state.text}
onBlur={this.onFloatLabel} />
**我刪除了靜態函數和它的工作。
你究竟在做什麼?訪問輸入字段的值? – azium
@azium,我在我的組件內部有一個靜態函數,我想從外部調用它,但我需要以某種方式來引用此組件。 –
通常在React中使用refs意味着你做錯了什麼。你可以發佈你的組件的靜態方法和組件要調用它從?幾乎肯定有更好的方法 – azium