Javascript 如何在OnChangeText中向我的组件inputText添加两种状态

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

我需要在onChangeText中添加两个参数,但我不知道如何才能做到这一点

我的组成部分是:

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
  • TextInput
    表示电话号码或DDD
  • 根据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)}