Flex 属性未应用于 TouchableOpacity

Iza*_*ert 3 react-native react-native-android

我正在关注教程并被动态TouchableOpacity元素卡住了。出于某种原因,没有应用 flex 属性。当我使用 chrome 的开发工具查看元素时,我可以看到它们,但它们没有 flex 属性。如果我手动添加它,我会得到我想要的外观。

这是render片段:

render() {
        const { rgb, size } = this.state;
        const { width } = Dimensions.get('window');

        return (
            <View style={styles.container}>
                <Header fontSize={40} />
                <View
                    style={{
                        height: width * 0.875,
                        width: width * 0.8758,
                        flexDirection: 'row',
                    }}
                >
                    {Array(size)
                        .fill()
                        .map((val, columnIndex) => (
                            <View
                                style={{ flex: 2, flexDirection: 'column' }}
                                key={columnIndex}
                            >
                                {Array(size)
                                    .fill()
                                    .map((val, rowIndex) => (
                                        <TouchableOpacity
                                            key={`${rowIndex}.${columnIndex}`}
                                            style={{
                                                flex: 1,
                                                backgroundColor: `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`,
                                                margin: 2,
                                            }}
                                            onPress={() =>
                                                console.log(
                                                    rowIndex,
                                                    columnIndex
                                                )
                                            }
                                        />
                                    ))}
                            </View>
                        ))}
                </View>
            </View>
        );
    }
Run Code Online (Sandbox Code Playgroud)

Iza*_*ert 10

愚蠢的错误。TouchableOpacity从错误的包中导入。检查您是否从react-native

  • 哇,这里也一样!感谢您发布此问题和答案。VS Code 的自动导入使这件事变得非常容易。 (5认同)