【问题标题】:React Native - Picker not showing up even after writing the correct codeReact Native - 即使编写了正确的代码,选择器也没有出现
【发布时间】:2019-07-20 22:44:24
【问题描述】:

我是 React Native 的新手,正在学习一门课程。不幸的是,我遇到了我的选择器没有出现的问题。即使在设计了我的 CardSection 之后,它也无法正常工作。这是代码。请帮忙

我尝试过设置 CardSection 的样式,但它似乎不起作用

import React, { Component } from 'react';
import { Picker, Text } from 'react-native';
import { connect } from 'react-redux';
import { employeeUpdate } from '../actions';
import { Card, CardSection, Input, Button } from './common';

class EmployeeCreate extends Component {
    render() {
        return (
            <Card>
                <CardSection>
                    <Input
                        label='Name'
                        placeholder='Jane'
                        value={this.props.name}
                        onChangeText={value => this.props.employeeUpdate({ prop: 'name', value })}
                    />
                </CardSection>

                <CardSection>
                    <Input
                        label='Phone'
                        placeholder='555-555-5555'
                        value={this.props.phone}
                        onChangeText={value => this.props.employeeUpdate({ prop: 'phone', value })}
                    />
                </CardSection>

                <CardSection style={{ flexDirection: 'column' }}>
                    <Text style={styles.pickerTextStyle}>Shift</Text>
                    <Picker
                        style={{ flex: 1 }}
                        selectedValue={this.props.shift}
                        onValueChange={value => this.props.employeeUpdate({ prop: 'shift', value })}
                    >
                        <Picker.Item label="Monday" value="Monday" />
                        <Picker.Item label="Tuesday" value="Tuesday" />
                        <Picker.Item label="Wednesday" value="Wednesday" />
                        <Picker.Item label="Thursday" value="Thursday" />
                        <Picker.Item label="Friday" value="Friday" />
                        <Picker.Item label="Saturday" value="Saturday" />
                        <Picker.Item label="Sunday" value="Sunday" />
                    </Picker>
                </CardSection>

                <CardSection>
                    <Button>Create</Button>
                </CardSection>
            </Card>
        );
    }
}

const styles = {
    pickerTextStyle: {
        fontSize: 18,
        paddingLeft: 20
    }
};

const mapStateToProps = (state) => {
    const { name, phone, shift } = state.employeeForm;

    return { name, phone, shift };
};

export default connect(mapStateToProps, { employeeUpdate })(EmployeeCreate);

这里是卡片部分

import React from 'react';
import { View } from 'react-native';

const CardSection = (props) => {
    return (
        <View style={[styles.constainerStyle, props.style]}>
            {props.children}
        </View>
    );
};

const styles = {
    constainerStyle: {
        bottomBorderWidth: 1,
        padding: 5,
        backgroundColor: '#FFF',
        justifyContent: 'flex-start',
        flexDirection: 'row',
        borderColor: '#DDD',
        position: 'relative'
    }
};

export { CardSection };

我希望 CardSection 像 Picker 一样显示

【问题讨论】:

  • 是否可以将flex: 1 添加到 CardSection?因为如果 CardSection 是父级,它需要有一些宽度和高度,所以 Picker 可以使用 flex: 1

标签: react-native


【解决方案1】:

找到了解决办法。将以下样式添加到选择器可以解决它

style={{ alignItems: "center" }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 2015-11-07
    • 2021-11-28
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2023-01-30
    • 2019-03-26
    相关资源
    最近更新 更多