【问题标题】:How can I render data vertically in React Native?如何在 React Native 中垂直渲染数据?
【发布时间】:2021-07-23 23:41:00
【问题描述】:

一种产品有多个variations,我想将它们全部呈现在 FlatList 或 SectionList 中:

JSON:

[
    {
       ...
    },
    {
        "id": 2,
        "title": "chicken burger 2",
        "sort": 2,
        "img": "http://127.0.0.1:8000/media/categories/unnamed_tyEgUqN.jpg",
        "price": 2.0,
        "category": {
            "id": 1,
            "title": "sandwiches",
            "img": "http://127.0.0.1:8000/media/categories/original_D3hTuRE.jpg"
        },
        "description": "chicken burger chicken burger chicken burger chicken burger chicken burger chicken burger chicken burger chicken burger chicken burger chick",
        "variations": [
            {
                "id": 1,
                "variation_category": {
                    "id": 1,
                    "title": "add ons",
                    "is_optional": true,
                    "is_selectable": false
                },
                "item": "extra cheese",
                "price": 0.1
            },
            {
                "id": 2,
                "variation_category": {
                    "id": 1,
                    "title": "add ons",
                    "is_optional": true,
                    "is_selectable": false
                },
                "item": "double chicken",
                "price": 0.6
            }
        ]
    }
]

我通过此代码获得了产品的所有变体:

function ProductDetailScreen(props) {
    //api
  const [productsData, setProductsData] = useState([]);
  useEffect(() => {
    loadProductsData();
  }, []);

  const loadProductsData = async () => {
    const response = await productsApi.getProducts();
    setProductsData(response.data);
  };
  const productId = props.route.params.productId;
  const thisProduct = productsData.filter((prod) => prod.id === productId);
const variations = thisProduct.map((product) =>
    product.variations.map((variation) => variation.item)
  );
return (
    
    ...
      {variations &&
    variations.map((variation, index) => (
      <View key={index}>
        <Addons item={variation} />
      </View>
    ))}
...
)

但它们在移动屏幕上看起来像这样 =>: horizontally

我的问题是如何将它们垂直渲染成这样:

extra cheese 0.100
double chicken 0.600

Edit: 按要求添加组件:

import React from "react";
import { StyleSheet, TouchableOpacity } from "react-native";
// Config
import colors from "../config/colors";
import MyText from "../config/MyText";
// Icons
import { Feather } from "@expo/vector-icons";

function Addons(props) {
  return (
    <TouchableOpacity onPress={props.onPress} style={styles.card}>
        <Feather name="check-square" size={24} color="black" />
        <MyText padding={10}>{props.item}</MyText>
        <MyText padding={10}>{props.price}</MyText>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  card: {
    height: 50,
    width: 300,
    backgroundColor: colors.white,
    borderBottomWidth: 1,
    borderBottomColor: colors.disabled,
    justifyContent: "space-between",
    alignItems: "center",
    flexDirection: "column",
    alignSelf: "center"
  },
});

export default Addons;

提前谢谢你,

【问题讨论】:

  • 不会有flex-direction:column 帮助吗?
  • 是的,不会的。

标签: javascript react-native react-native-flatlist


【解决方案1】:

插件组件内的主视图应具有以下样式

container: 
{ 
  flex: 1, 
  alignItems: "center", 
  justifyContent: "center", 
  flexDirection: "column", 
},

编辑:下面的代码块,可以垂直获取行

 <View style={{ flex:1, flexDirection:"column" }}>
   variations.map((variation, index) => (
   <View key={index} style={{ flex:1 }}>
     <Addons item={variation} />
   </View>
</View>

这应该可以解决您的问题

【讨论】:

  • 你能提供插件的代码以便更清楚吗?请在主帖中添加作为编辑
  • 当然。已添加
  • 请看我回答的编辑部分。
猜你喜欢
  • 2017-08-08
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 2019-08-14
  • 2015-06-02
  • 1970-01-01
相关资源
最近更新 更多