简体   繁体   English

反应:将多个相似的输入字段保存为一种状态

[英]React: Save multiple similar inputfields in one state

I have three same input fields that are masked to have phone format text. 我有三个相同的输入字段,这些字段被屏蔽为具有电话格式的文本。 All input fields are exactly the same(Phone input field).now I have three states, with three handlePhoneNumber methods. 所有输入字段都完全相同(“电话输入”字段)。现在,我具有三种状态以及三种handlePhoneNumber方法。 This is not a good practice since all these fields are exactly the same basically. 这不是一个好习惯,因为所有这些字段基本上都完全相同。

The UI library I used is Material-UI and i used React-Text-Mask library for my mask component. 我使用的UI库是Material-UI而我React-Text-Mask组件使用了React-Text-Mask库。

What I have so far: 到目前为止,我有:

this.state = {
    textmask: "(   )    -    ",
    textmask2: "(   )    -    ",
    textmask3: "(   )    -    ",
}

RenderTextMask(props){
    const { inputRef, ...other } = props;
    return (
      <MaskedInput
        {...other}
        ref={inputRef}
        mask={['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]}
        guide={true}
        showMask
      />
    );
}

My OutlienInput component(from material-UI), looks like this: 我的OutlienInput组件(来自material-UI)如下所示:

        <div>
            <FormLabel>Phone</FormLabel>
            <div>
                <OutlinedInput
                    value={this.state.textmask}
                    onChange={this.handlePhoneNumber}
                    inputComponent={this.RenderTextMask}
                    labelWidth={200}
                />
            </div>
        </div>
        <div>
            <FormLabel>Phone</FormLabel>
            <div>
                <OutlinedInput
                    value={this.state.textmask2}
                    onChange={this.handlePhoneNumber2}
                    inputComponent={this.RenderTextMask}
                    labelWidth={200}
                />
            </div>
        </div>
        <div>
            <FormLabel>Phone</FormLabel>
            <div>
                <OutlinedInput
                    value={this.state.textmask3}
                    onChange={this.handlePhoneNumber3}
                    inputComponent={this.RenderTextMask}
                    labelWidth={200}
                />
            </div>
        </div>

What the problem is: So far I need one state and one handle method for every phone field that I have. 问题是什么:到目前为止,我需要为每个电话字段提供一种状态和一种处理方法。 As you can see, as the app grows, the code is not really maintainable. 如您所见,随着应用程序的增长,该代码实际上是不可维护的。

What I want to achieve: I want to have one state and one handle method that handles all three fields(possibly more fields in the future). 我想要实现的目标:我想拥有一个状态和一个处理所有三个字段(将来可能会有更多字段)的handle方法。 Or something better than what I currently have. 还是比我现在拥有的更好的东西。 So in the future, if I have to add more fields, i don't have to modify state and add new method every time. 因此,在将来,如果我必须添加更多字段,则不必每次都修改状态并添加新方法。

What I have tried so far I tried to have array testmask as state. 到目前为止,我尝试过将数组testmask作为状态。 The problem is that since I used react-text-mask as my mask library. 问题在于,由于我使用react-text-mask作为我的遮罩库。 it only accepts String. 它只接受String。 If I change " textmask " into array of strings, it will prompts error. 如果我将“ textmask ”更改为字符串数组,则会提示错误。

With a little guess, I would say that array is natural approach. 稍作猜测,我会说数组是自然的方法。 There is just part of code, and I might be wrong. 只有一部分代码,我可能是错的。

IMO, you could do this: IMO,您可以这样做:

this.state = {
    textmasks: ["(   )    -    ", "(   )    -    ", "(   )    -    "]
}

as you mentioned already. 正如您已经提到的。 Now, in your render method, something like this: 现在,在您的render方法中,如下所示:

const phoneInputs = [];
for (let i = 0; i < 3; i++) {
       phoneInputs.push(<div key={i}>
            <FormLabel>Phone</FormLabel>
            <div>
                <OutlinedInput
                    value={this.state.textmasks[i]}
                    onChange={this.handlePhoneNumber.bind(this, i)}
                    inputComponent={this.RenderTextMask}
                    labelWidth={200}
                />
            </div>
        </div>);
}

So, for this to work, you use this.state.textmasks[i] (input index) to catch appropriate mask. 因此,为了使其正常工作,您可以使用this.state.textmasks [i](输入索引)来捕获适当的掩码。 Also, to method this.handlePhoneNumber, you are binding index "i". 另外,要绑定this.handlePhoneNumber方法,您需要绑定索引“ i”。

I wrote "this.state.textmasks[i]" and "this.handlePhoneNumber.bind(this, i)", just because I assumed that your implementation depends on phone number input index (not only to store to appropriate place, but to do some additional logic also). 我写了“ this.state.textmasks [i]”和“ this.handlePhoneNumber.bind(this,i)”,只是因为我假设您的实现取决于电话号码输入索引(不仅存储在适当的位置,而且存储在也可以执行一些其他逻辑)。

If you will only use one text mask and only store list of phone numbers (I assume in handlePhoneNumber method), then you most likely should have: 如果仅使用一个文本掩码并且仅存储电话号码列表(我假设在handlePhoneNumber方法中),那么您很可能应该具有:

const textMask = "(   )    -    ";
this.state = {
    phoneNumbers: []
};

This way, textMask wouldn't even be state/index dependent (what most likely is your business case) and phoneNumbers would be handled in onChange trigger (handlePhoneNumber method) which takes index as first parameter, so it can update appropriate phoneNumbers index. 这样,textMask甚至不依赖于状态/索引(很可能是您的业务案例),并且将在将索引作为第一个参数的onChange触发器(handlePhoneNumber方法)中处理phoneNumbers,因此它可以更新适当的phoneNumbers索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM