【问题标题】:how to map array of array into react native picker如何将数组数组映射到反应本机选择器
【发布时间】:2020-12-02 07:34:15
【问题描述】:

如何将属性数组(产品的子数组)映射到选择器中。 这是我的 JSON 数据:

{ "products": [ { "productid": "5466", "name": "Cashew", “描述”:“”,“属性”:[{“product_id”:“5466”, “attribute_name”:“500 grm”,“attribute_price”:“500”},{ “product_id”:“5466”、“attribute_name”:“250 grm”、“attribute_price”: "250" } ] } ] }

这是我的代码:

renderProductsCart = ({ item }) => {
    return (
      <View style={{ width: "46%", marginLeft: 10, marginTop: 10 }}>
        <Card
          elevation={2}>
          <Card.Cover source={{ uri: item.image }} />
          <Text style={{ marginLeft: 5 }}>{item.name}</Text>
          <Text> ₹: {item.attribute_price}/- </Text>
          <Picker
            selectedValue={this.state.PickerValueHolder}

            onValueChange={(itemValue, itemIndex) => this.setState({ PickerValueHolder: itemValue })} >

            {this.state.data.products.attributes.map((item, key) => (
                        <Picker.Item label={item.attribute_name} value={item.attribute_name} key={item} />)
                    )}
            
          </Picker>
          <View style={{ flexDirection: "row" }}>
            <Card.Actions>
              <Button
                theme={{ colors: { primary: 'black' } }}
                onPress={() => console.log("press")}>View More</Button>
              <Button
                theme={{ colors: { primary: 'black' } }}
                onPress={() => console.log("press")}>Cart</Button>
            </Card.Actions>
          </View>
        </Card>
      </View>
    )
  }

       <FlatList
            data={this.state.data.products}
            renderItem={this.renderProductsCart}
            keyExtractor={(item, index) => index.toString()}
            numColumns={2}
          />

请帮帮我 提前致谢

【问题讨论】:

    标签: react-native mapreduce react-native-android react-native-flatlist react-native-picker-select


    【解决方案1】:

    我在这里使用了功能组件,但您也可以使用基于类的组件并以这种方式使用它: 输出:

    完整示例代码:

    import React, {useState, useEffect} from 'react';
    import { Text, View, StyleSheet, Picker, FlatList, Button } from 'react-native';
    import Constants from 'expo-constants';
    
    // You can import from local files
    import AssetExample from './components/AssetExample';
    
    // or any pure javascript modules available in npm
    import { Card } from 'react-native-paper';
    const dataP = {
      products: [
        {
          productid: '5466',
          name: 'Cashew',
          description: '',
          attributes: [
            {
              product_id: '5466',
              attribute_name: '500 grm',
              attribute_price: '500',
            },
            {
              product_id: '5466',
              attribute_name: '250 grm',
              attribute_price: '250',
            },
          ],
        },
      ],
    };
    
    export default function App() {
      const [data, setData] = useState({});
    
      useEffect(() => {
        setData(dataP);
      }, []);
    
      const renderProductsCart = ({ item }) => {
        return (
          <View style={{ width: '46%', marginLeft: 10, marginTop: 10 }}>
            <Card elevation={2}>
              <Card.Cover source={{ uri: item.image }} />
              <Text style={{ marginLeft: 5 }}>{item.name}</Text>
              <Text> ₹: {item.attribute_price}/- </Text>
              <Picker
                selectedValue={''}
                onValueChange={(itemValue, itemIndex) => this.setState({})}>
                {item?.attributes.map((item, key) => (
                  <Picker.Item
                    label={item.attribute_name}
                    value={item.attribute_name}
                    key={item}
                  />
                ))}
              </Picker>
              <View style={{ flexDirection: 'row' }}>
                <Card.Actions>
                  <Button
                    theme={{ colors: { primary: 'black' } }}
                    onPress={() => console.log('press')}>
                    View More
                  </Button>
                  <Button
                    theme={{ colors: { primary: 'black' } }}
                    onPress={() => console.log('press')}>
                    Cart
                  </Button>
                </Card.Actions>
              </View>
            </Card>
          </View>
        );
      };
      return (
        <View style={styles.container}>
          <FlatList
            data={dataP?.products}
            renderItem={renderProductsCart}
            keyExtractor={(item, index) => index.toString()}
            numColumns={2}
          />
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
        padding: 8,
      },
      paragraph: {
        margin: 24,
        fontSize: 18,
        fontWeight: 'bold',
        textAlign: 'center',
      },
    });
    

    工作Expo Snack演示

    【讨论】:

    • 但是类组件不支持钩子,对吧?而且我正在通过 fetchAPI 检索 JSON 数据,所以我认为对 JSON 数据进行硬编码是不必要的,如果我们这样做,我们需要在数据库中的每个新条目之后更新 JSON 数据。
    • 钩子用于功能组件,您可以将硬编码的数据替换为从 API 获取的数据。
    • 我该怎么做?
    • 在功能组件的情况下,使用axios或fetch API从服务器获取数据,将其分配给状态,在基于类的组件的情况下,您可以遵循相同的方法componentDidMount,例如:stackoverflow.com/a/65080708/5669120
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2023-04-04
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多