【问题标题】:Collect TextInput in separate Component in React Native在 React Native 中的单独组件中收集 TextInput
【发布时间】:2017-06-25 07:01:26
【问题描述】:

我在 React Native 中有一些与信用卡相关的 TextInputs。我想将这些 TextInputs 收集到一个单独的组件中。

所以它看起来像

class CreditCard extends Component {
  state = { number: null, expirationMonth: null, expirationYear: null, cvc: null };

  render() {
    return (
      <View>
        <TextInput onChangeText={...} ... />
        <TextInput onChangeText={...} ... />
        <TextInput onChangeText={...} ... />
      </View>
    );
  }
}

在我的应用程序中,我正在导入信用卡组件并像使用它一样

class AddCreditCard extends Component {
  render() {
    return (
      <View>
        <CreditCard ... />
        <Button disabled={creditCardIsInvalid} ... />
      </View>
    );
  }
}

我知道如何将数据保存在信用卡组件的状态中,但是如何将数据从信用卡组件提取到容器组件(AddCreditCard)?

我想这类似于将creditCard 对象添加到容器并将onChange 属性添加到CreditCard,所以我可以直接从CreditCard 组件填充creditCard 对象?

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    您可以将状态移动到 AddCreditCard 组件并将一个 prop 传递给将更新容器状态的 CreditCard 组件。 这样你就可以通过state和props里面的CreditCard访问AddCreditCard组件中的数据

    class AddCreditCard extends Component {
    
        state = { number: null, expirationMonth: null, expirationYear: null, cvc: null };
    
        textFieldChanged = (key, value) => {
            let newState = {};
            newState[key] = value;
            this.setState(newState);
        }
    
        render() {
            return (
                <View>
                    <CreditCard 
                        textFieldChanged={this.textFieldChanged} 
                        number={this.state.number} 
                        expirationMonth={this.state.expirationMonth} 
                        expirationYear={this.state.expirationYear} 
                        cvc={this.state.cvc} 
                    />
                    <Button disabled={creditCardIsInvalid} ... />
                </View>
            );
        }
    }
    

    然后你可以这样称呼它:

    class CreditCard extends Component {
        render() {
            // this.props.number ...
            return (
                <View>
                    <TextInput 
                        onChangeText={(text) => {
                            this.props.textFieldChanged('number', text)
                        }}
                    />
                    <TextInput 
                        onChangeText={(text) => {
                            this.props.textFieldChanged('expirationMonth', text)
                        }}
                    />
                    <TextInput 
                        onChangeText={(text) => {
                            this.props.textFieldChanged('expirationYear', text)
                        }}
                    />
                    <TextInput 
                        onChangeText={(text) => {
                            this.props.textFieldChanged('cvc', text)
                        }}
                    />
                </View>
            );
        }
    }
    

    【讨论】:

    • 谢谢!但是我如何才能定义creditCardIsInvalid?我是否必须在所有带有&lt;CreditCard /&gt; 的容器中执行此操作?另外,我需要将初始数据从AddCreditCard 传递给CreditCard - 所以我必须在CreditCard 中使用state = { number: this.props.number, ... } 吗?
    • 是的,是的! :)
    猜你喜欢
    • 2019-01-15
    • 2018-08-07
    • 1970-01-01
    • 2021-12-27
    • 2020-12-29
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多