Javascript 如何在OnChangeText中向我的组件inputText添加两种状态
我需要在onChangeText中添加两个参数,但我不知道如何才能做到这一点 我的组成部分是:Javascript 如何在OnChangeText中向我的组件inputText添加两种状态,javascript,reactjs,react-native,components,react-component,Javascript,Reactjs,React Native,Components,React Component,我需要在onChangeText中添加两个参数,但我不知道如何才能做到这一点 我的组成部分是: handleChangePhone = (value) => { this.setState(prevState => ({ phone: normalizePhone(value, prevState.phone) })) } handleChangeDDD = (value) => { this.setState(prevState => ({ ddd: n
handleChangePhone = (value) => {
this.setState(prevState => ({ phone: normalizePhone(value, prevState.phone) }))
}
handleChangeDDD = (value) => {
this.setState(prevState => ({ ddd: normalizeDDD(value, prevState.phone) }))
}
setPasswordVisibility = () => {
this.setState({ hidePassword: !this.state.hidePassword })
}
render() {
const { value, onChangeValue } = this.props;
return (
<>
<TextInput
{...this.props}
onChangeText={onChangeValue, this.props.phone ? this.handleChangePhone : this.props.ddd ? this.handleChangeDDD : onChangeValue}
value={value}
defaultValue={this.props.phone ? this.state.phone : this.props.ddd ? this.state.ddd : ''}
placeholder={this.props.placeholder ? this.props.placeholder : ''}
selectionColor='#6730EC'
keyboardType={this.props.keyboard ? this.props.keyboard : 'default'}
maxLength={this.props.maxLen ? this.props.maxLen : 100}
style={[styles.input,
{
width: this.props.width ? this.props.width : 244,
marginLeft: this.props.marginL ? this.props.marginL : 0,
marginRight: this.props.marginR ? this.props.marginR : 0,
marginTop: this.props.marginT ? this.props.marginT : 0,
textAlign: this.props.alignText ? this.props.alignText : 'left',
fontSize: this.props.fontSize ? this.props.fontSize : 15,
}]}
secureTextEntry={this.props.type == 'security' ? this.state.hidePassword : false}
ref={(input) => this.props.inputRef && this.props.inputRef(input)}
autoFocus={this.props.focus ? this.props.focus : false}
//onSubmitEditing={this.handleTitleInputSubmit}
/>
<Feather style={[styles.eye,
{
marginTop: this.props.marginT ? this.props.marginT : 0,
}]}
name={(this.state.hidePassword) ? 'eye' : 'eye-off'}
size={this.props.eye ? 24 : 0}
color="#6730EC"
onPress={this.setPasswordVisibility}
/>
</>
handleChangePhone=(值)=>{
this.setState(prevState=>({phone:normalizePhone(value,prevState.phone)}))
}
handleChangeDDD=(值)=>{
this.setState(prevState=>({ddd:normalizeDDD(value,prevState.phone)}))
}
setPasswordVisibility=()=>{
this.setState({hidePassword:!this.state.hidePassword})
}
render(){
const{value,onChangeValue}=this.props;
返回(
this.props.inputRef&&this.props.inputRef(输入)}
自动对焦={this.props.focus?this.props.focus:false}
//onSubmitEditing={this.handleitleInputSubmit}
/>
handledd和Handle CelNumber函数是call,其中我的param phone为true,但我需要这个带有onChangeValue的更改状态,但我的方式不起作用
你能帮我吗?从你的代码来看,它似乎是:
- 您可以从
获取道具this.props
和value
onChangeValue
表示电话号码或DDDTextInput
- 根据if
这一点,我们知道它是电话号码还是DDD。道具包括
或电话
DDD
this.props.onChangeValue
我不知道您的函数normalizePhone
和normalizedd
在做什么。您可能只想在获得通过验证的值时调用父级。但这与我在这里看到的不兼容,即您正在将文本输入的值设置为此.props.value
handleChangeText = (text) => {
const prevText = this.props.value;
const normalized = this.props.phone
? normalizePhone(text, prevText)
: this.props.ddd
? normalizeDDD(text, prevText)
: text;
this.props.onChangeValue(normalized);
};
render() {
return (
<>
<TextInput
{...this.props}
onChangeText={this.handleChangeText}
value={this.props.value}
defaultValue={""}
....
handleChangeText=(text)=>{
const prevText=this.props.value;
const normalized=this.props.phone
?标准化电话(文本、前置文本)
:this.props.ddd
?规范化的EDDD(文本,前置文本)
:文本;
this.props.onChangeValue(标准化);
};
render(){
返回(
请向我们展示整个组件并解释为什么需要这些组件:onChangeText={onChangeValue,this.props.phone?this.handleChangePhone:this.props.ddd?this.handlechangedd:onChangeValue}
和ref={(input)=>this.props.inputRef&&this.props.inputRef(input)}