组件未在react-native中呈现

时间:2018-10-07 08:49:12

标签: javascript reactjs react-native react-native-android

尽管我在React方面很有经验,但我对react-native还是陌生的。对于相同的问题,我已经尝试过在SO中发布几个答案,但是没有一个可以帮助我解决问题。

我有App组件,App组件调用了Account组件。 “帐户”组件呈现输入字段,但输入字段未在UI中显示,但是如果我仅在<Text>Hello</Text>之类的App组件中添加文本,它将显示在UI中,但我的自定义帐户组件未显示在UI中。我不知道我在做什么错。

PFB组件详细信息

App.js

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Account from './components/Form/components/Account';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Account />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Account.js

import React, { Component } from "react";
import { StyleSheet, Text, View, Button, TextInput, ScrollView } from 'react-native';

export default class Account extends Component {
    constructor(props){
        super(props);
        this.state = {
            cardNo: "",
            amount: 0
        }
    }

    handleCardNo = no => {
        this.setState({
            cardNo: no
        });
    }

    handleAmount = amount => {
        this.setState({
            amount
        });
    }

    handleSend = event => {
        const { cardNo, amount } = this.state;
        this.setState({
            loading: true
        });
        const regex = /^[0-9]{1,10}$/;
        if(cardNo == null){

        }else if (!regex.test(cardNo)){
            //card number must be a number
        }else if(!regex.test(amount)){
            //amount must be a number
        }else{
            // const obj = {};
            // obj.cardNo = cardNo;
            // obj.amount = amount;
            // const url = "http://localhost:8080/apple"
   //          axios.post(url, obj)
   //          .then(response => return response.json())
   //          .then(data => this.setState({
   //           success: data,
   //           error: "",
   //           loading: false
   //          }))
   //          .catch(err => {
   //           this.setState({
   //               error: err,
   //               success: "",
   //               loading: false
   //           })
   //          })
            //values are valid
        }

    }

    render(){
        const { cardNo, amount } = this.state;
        return(
            <View>
                <TextInput label='Card Number' style={{height: 40, flex:1, borderColor: '#333', borderWidth: 1}} value={cardNo} onChangeText={this.handleCardNo} />
                <TextInput label='Amount'  style={{height: 40, flex:1, borderColor: '#333', borderWidth: 1}} value={amount} onChangeText={this.handleAmount} />
                <Button title='Send' onPress={this.handleSend}/>
            </View>
        )
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在一些样式问题。尝试以此更改渲染功能

render() {
    const { cardNo, amount } = this.state
    return (
      <View style={{ alignSelf: 'stretch' }}>
        <TextInput
          placeholder="Card Number"
          style={{ height: 40, borderColor: '#333', borderWidth: 1 }}
          value={cardNo}
          onChangeText={this.handleCardNo}
        />
        <TextInput
          placeholder="Amount"
          style={{ height: 40, borderColor: '#333', borderWidth: 1 }}
          value={amount}
          onChangeText={this.handleAmount}
        />
        <Button title="Send" onPress={this.handleSend} />
      </View>
    )
  }