react-native - 在 for 循环内使用 <Text> 时的填充/边距

标签 react-native react-native-ios

我正在使用 for 循环创建多个 Text,但是当我使用这种方法时,我看到了一些不必要的填充。

render(){
    var child = [];
    for (let index = 0; index < 5; index++) {
        child.push(<Text key={index}> {index}</Text >)
    }

    return (
        <View>
            <View style={{ flexDirection: 'row' }}>
                {child}
            </View>
            <View style={{ flexDirection: 'row' }}>
                <Text>0</Text>
                <Text>1</Text>
                <Text>2</Text>
                <Text>3</Text>
                <Text>4</Text>
            </View>
        </View>
    )
}

输出:

enter image description here

正如您可以观察到的,我通过 for 循环添加的 Text 在两个 Text 之间留有一些空格,但是如果我连续写入 5 次,它就可以工作很好。

最佳答案

此时你错过了一些东西。您的循环中有一个空格。

child.push(<Text key={index}> {index}</Text >)

改成这样

child.push(<Text key={index}>{index}</Text >)

关于react-native - 在 for 循环内使用 <Text> 时的填充/边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383867/

相关文章:

svg - 如何在 React-native 或 Expo 中使用 SVG-Uri?

ios - 我在 iOS 上运行 react native 应用程序时遇到奇怪的问题

react-native - 我如何检查安装在 React Native 代码中的应用程序

react-native - 将流浏览器与 expo 一起使用

ios - 未找到无法识别的模块映射文件 Release-iphonesimulator/YogaKit/YogaKit.modulemap'

android - "React native run-android"连接超时

ios - code-push react-native 会覆盖 iOS 版本并将旧的推送部署到新版本吗?

javascript - 数据未在 React Native 中使用 Flatlist 渲染

javascript - 如何在 react-native 项目中启用 jsconfig.json

javascript - 在两个可拖动的 Flatlist-React Native 之间拖放