【问题标题】:Can't showcase values within nested array in mobx storage on Flatlist无法在 Flatlist 上的 mobx 存储中显示嵌套数组中的值
【发布时间】:2019-09-22 22:56:05
【问题描述】:

我正在尝试显示嵌套数组中的值,显示来自第一层的值,如 item.prositem.name 有效,但是当我想显示来自嵌套数组的值时,如 item.uses.uses1 它不显示任何内容并且当我尝试使用[index] 时,它会将其描述为未定义。如何显示相关值?感谢您的宝贵时间

下面的mobx数组:

import {observable, action} from 'mobx';
import {LayoutAnimation} from 'react-native'

class StateStorage {


  // Arrays
  @observable list= ['Category','','','']
  @observable selectedMaterial=''
  @observable  materials = [ 
    {


      name: 'Nap',
      uses:[
    {  uses1:'- Utility'}, 
     { uses2:'- Healthy' },
     { uses3:'- Timeful'},
      {uses4:''},
      {uses5:''},
      {uses6:''},
      {uses7:''},
      {uses8:''},
      {uses9:''},
      {uses10:''},
                    ],

      pros:'',
     cons:'',


    },
    {

      name: 'Cat',
      uses:[
    {  uses1:'- Live'}, 
     { uses2:'- Encounter' },
     { uses3:'- Playful'},
      {uses4:''},
      {uses5:''},
      {uses6:''},
      {uses7:''},
      {uses8:''},
      {uses9:''},
      {uses10:''},
                    ],

      pros:'',
     cons:'',
  ],
        name: 'Event',
      uses:[
    {  uses1:'- Hidden'}, 
     { uses2:'- Opportunist' },
     { uses3:'- Wasteful'},
      {uses4:''},
      {uses5:''},
      {uses6:''},
      {uses7:''},
      {uses8:''},
      {uses9:''},
      {uses10:''},
                    ],
      pros:'',
     cons:'',


   },
]


 for (i = 0; i < this.materials.length; i++)
              {
                for (n=0; n<this.materials[i].uses.length; n++){
                if(this.materials[i].uses[n].uses1===''
                || this.materials[i].uses[n].uses2===''
                || this.materials[i].uses[n].uses3===''
                || this.materials[i].uses[n].uses4===''
                || this.materials[i].uses[n].uses5===''
                || this.materials[i].uses[n].uses6===''
                || this.materials[i].uses[n].uses7===''
                || this.materials[i].uses[n].uses8===''
                || this.materials[i].uses[n].uses9===''
                || this.materials[i].uses[n].uses10===''
              //&& this.materials[i].count < 10
              )
              {this.materials[i].count++}}
                      }

              }

React-native Flatlist:

@observer
class App extends Component {
CommonUseViewStyle(index){



    StateStorage.incrementCount()



if(StateStorage.materials[index].count === StateStorage.materials.length*10
  ||  StateStorage.materials.length*9
  ||  StateStorage.materials.length*8) 
{return{height:0}}

else if( StateStorage.materials[index].count === StateStorage.materials.length*7)
{return{height:15}}

else if (StateStorage.materials[index].count === StateStorage.materials.length*6 )
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 30 : 31}}

else if(StateStorage.materials[index].count === StateStorage.materials.length*5 )
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 45 :47}}

else if(StateStorage.materials[index].count === StateStorage.materials.length*4 )
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 60 : 63}}

else if(StateStorage.materials[index].count === StateStorage.materials.length*3
)
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 75 : 79}}

else if(StateStorage.materials[index].count === StateStorage.materials.length*2
)
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 90 : 95}}

else if(StateStorage.materials[index].count === StateStorage.materials.length
  || StateStorage.materials[index].count === 0
)
{return{height:PixelRatio.get()<=2 && Platform.OS==='android' ? 105 : 111}}

}




ProsViewStyle(index){

  if(StateStorage.materials[index].pros4 ==='') 
  {return{height:40}}

  else if(StateStorage.materials[index].pros4 !==''
  && StateStorage.materials[index].pros5 ==='')
  {return{height:70}}

  else if(StateStorage.materials[index].pros4 !=='' 
  && StateStorage.materials[index].pros5 !==''
  && StateStorage.materials[index].pros6 ==='' )
  {return{height:140}}

  else if(StateStorage.materials[index].pros4 !=='' 
  && StateStorage.materials[index].pros5 !==''
  && StateStorage.materials[index].pros6 !==''
  && StateStorage.materials[index].pros7 ==='' )
  {return{height:165}}

  else if(StateStorage.materials[index].pros4 !=='' 
  && StateStorage.materials[index].pros5 !==''
  && StateStorage.materials[index].pros6 !==''
  && StateStorage.materials[index].pros7 !==''
  && StateStorage.materials[index].pros8 ==='' )
  {return{height:210}}

  else if(StateStorage.materials[index].pros4 !=='' 
  && StateStorage.materials[index].pros5 !==''
  && StateStorage.materials[index].pros6 !==''
  && StateStorage.materials[index].pros7 !==''
  && StateStorage.materials[index].pros8 !=='' 
  && StateStorage.materials[index].pros9 ==='' 
  )
  {return{height:250}}



  }
       render() {





    return (

      <View
      style={{
        backgroundColor:'#262A2C',
        flex:1
      }}>
      <FlatList
     style={{marginTop:80,}}


     initialNumToRender={StateStorage.materials.length}

        data={StateStorage.materials}
        renderItem={({ item, index }) => (
       <View 

       >
         <TouchableOpacity
         onPress={() =>{
           StateStorage.chooseMaterial(index),
           console.log(StateStorage.materials[index].count) 




         }}>
          <ImageBackground
        // read computed                   
          source=require("./Icons/logout.png") 
        //pay FlatIcon or design personal one
          style={{ 

            resizeMode: 'contain',

          position:'relative',

          width: wp('100%'), 
          left: wp('0%'),
          borderBottomWidth: 1,
          borderBottomColor: 'grey',
          padding: hp('6%'),
          }}
        >
       <View
            style={{
              flex:1,
              height: null,
             //height:hp('19%'),
             width: null,
             //width: wp('100%'),
             //height:hp('21%'),

              borderBottomColor: 'grey',

            }}>


               <Image
               style={{
               width:wp('10%'),
               height:hp('5%'),
               left:wp('-10%'),
               top:hp('-5.5%'),
               //tintColor:'#81F018'
               }}
               source=require("./Icons/logout.png")/>
              <Image
               style={{
               width:wp('10%'),
               height:hp('5.5%'),
               left:wp('-10%'),
               top:hp('0%'),
               //tintColor:'#81F018'
               }}
               source=require("./Icons/logout.png")/>

               <Image
               style={{
               width:wp('10%'),
               height:hp('5.5%'),
               left:wp('-10%'),
               top:hp('6%'),
               //tintColor:'#81F018'
               }}
               source=require("./Icons/logout.png")}/>

              <Text 
              style={{ fontWeight: 'bold',
               fontSize: 22, 
              left:item.name.length<=5 ? wp('32.5%'):wp('27.5%'),
               top:hp('-9.5%'),

               //left:item.name.length<=5 ? wp('31.5%'):wp('26.5%'),
               //top:hp('-1.5%')
              //left:item.name.length<=5 ? wp('32.5%'):wp('27.5%'),
               //top:hp('-3.5%')
               }}>
               {item.name}
               </Text>
              <Text
              style={{
                fontWeight: 'bold',
                fontSize: 25, 
                top: hp('-25%'),
                left:wp('80%')
                // top: hp('-14%'),
                //left:wp('80%')
                //top: hp('-19%'),
                //left:wp('80%')
              }}>
              {item.price} 
              </Text>


          </View>






   </ImageBackground>
   </TouchableOpacity>
   <TouchableOpacity activeOpacity={0.8}  
   onPress={() => {

             StateStorage.changeLayout(index)

              }} 

    style={{ padding: 10,

    backgroundColor:'black',

    left:wp('-10.9%'),
    top:hp('0%'),
    width: wp('120%'),
    height:hp('5%')}}>
   <Image
               style={{
               width:wp('9%'),
               height:hp('4.5%'),
              tintColor:'white',
              left:250,
              top:-10
               //tintColor:'#81F018'
               }}
               source={StateStorage.materials[index].expanded ? require('../Icons/arrowDown.png') : require('../Icons/arrowUp.png') }/>
</TouchableOpacity>

<View style={{height: StateStorage.materials[index].expanded ? null : 0,
      overflow: 'hidden', 
      backgroundColor:'black' }}>
            <Text 
            style={{
               fontSize: 17,
               left:150,
               top:-10,
               color: 'turquoise',
               padding: 10}}>
          Specs
            </Text>
            <View >
             <Image
             style={{width:wp('8.5%'),
             height:hp('4%'),
             display: item.textSpec1==='' ? 'none' : 'flex',
            tintColor:'white'}}
              source={item.spec1}/> 
            <Text
             style={{
              fontSize: 17,
              left:50,
              top:-35,
               display: item.textSpec1==='' ? 'none' : 'flex',
              color: 'white',
              padding: 10}}>
{item.cons}
            </Text>
            <Image
             style={{width:wp('8.5%'),
             height:hp('4%'),
             top:-30,
             display: item.textSpec2==='' ? 'none' : 'flex',
            tintColor:'white'}}
              source={item.spec2}/> 
            <Text
             style={{
              fontSize: 17,
              left:50,
              top:-65,
              display: item.textSpec2==='' ? 'none' : 'flex',
              color: 'white',
              padding: 10}}>
{item.pros}
            </Text>
            <Image
             style={{width:wp('8.5%'),
             height:hp('4%'),
             display:item.textSpec3==='' ? 'none' : 'flex',
             top:-60,
            tintColor:'white'}}
              source={item.spec3}/> 
            <Text
             style={{
              fontSize: 17,
              display:item.textSpec3==='' ? 'none' : 'flex',
              left:50,
              top:-95,
              color: 'white',
              padding: 10}}>
{item.name}

            </Text>
            <Image 
            //category implementation
            />

            <Text>


            </Text>
            </View>

            <Text 
            style={{
              fontSize: 17 ,
               left:125,
               top:-90,
               color: 'turquoise',
               padding: 10}}>
          Common uses
            </Text>
            <View style={ 
              this.CommonUseViewStyle(index)

              //5height:110
            }>

            <Text
            //RE-EDIT CSS THAT IS MARGIN BASED RATHER THAN POSITION
            style=
             {{
           position:'absolute',
              fontSize: 14 ,
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-19%') : hp('-14%'),
              color: 'white',
             padding: 10

             }}>
{item.uses.uses1}
            </Text>
            <Text
             style={{
         position: 'absolute',
              fontSize: 14 ,
              display:item.uses.uses2==='' ? 'none' : 'flex', 
              //top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-23.5%') : hp('-17.5%'),
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-16%') : hp('-11%'),
              color: 'white',
              padding: 10}

               }
              >
{item.uses.uses2}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses3==='' ? 'none' : 'flex',
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-13%') : hp('-8%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses3}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses4==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-10%') : hp('-5%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses4}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses5==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-7%') : hp('-2%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses5}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses6==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-4%') : hp('1%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses6}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses7==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-1%') : hp('4%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses7}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses8==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('2%') : hp('7%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses8}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses9==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('5%') : hp('10%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses9}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses10==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('8%') : hp('13%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses10}
            </Text>
            </View>

          <Text
          style={{

              fontSize: 17 ,
                left:168,
                top:-40,
                color: 'turquoise',
                paddingTop: PixelRatio.get()<= 2 && Platform.OS ===' android' ? 55 :65,
                paddingBottom: 10



          }}>
)}}

显示有问题的部分:

 <Text 
            style={{
              fontSize: 17 ,
               left:125,
               top:-90,
               color: 'turquoise',
               padding: 10}}>
          Common uses
            </Text>
            <View style={ 
              this.CommonUseViewStyle(index)

              //5height:110
            }>

            <Text
            //RE-EDIT CSS THAT IS MARGIN BASED RATHER THAN POSITION
            style=
             {{
           position:'absolute',
              fontSize: 14 ,
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-19%') : hp('-14%'),
              color: 'white',
             padding: 10

             }}>
{item.uses.uses1}
            </Text>
            <Text
             style={{
         position: 'absolute',
              fontSize: 14 ,
              display:item.uses.uses2==='' ? 'none' : 'flex', 
              //top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-23.5%') : hp('-17.5%'),
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-16%') : hp('-11%'),
              color: 'white',
              padding: 10}

               }
              >
{item.uses.uses2}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses3==='' ? 'none' : 'flex',
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-13%') : hp('-8%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses3}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses4==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-10%') : hp('-5%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses4}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses5==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-7%') : hp('-2%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses5}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses6==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-4%') : hp('1%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses6}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses7==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('-1%') : hp('4%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses7}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses8==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('2%') : hp('7%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses8}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses9==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('5%') : hp('10%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses9}
            </Text>
            <Text
             style={{
              position:'absolute',
              fontSize: 14 ,
              display:item.uses.uses10==='' ? 'none' : 'flex',
              left:0,
              top: PixelRatio.get() <=3 && Platform.OS==='android' ? hp('8%') : hp('13%'),
              color: 'white',
              padding: 10

              } }
              >
{item.uses.uses10}
            </Text>
            </View>

          <Text
          style={{

              fontSize: 17 ,
                left:168,
                top:-40,
                color: 'turquoise',
                paddingTop: PixelRatio.get()<= 2 && Platform.OS ===' android' ? 55 :65,
                paddingBottom: 10



          }}>

【问题讨论】:

    标签: arrays react-native nested mobx react-native-flatlist


    【解决方案1】:

    试试地图功能。

    例如:-

    Array.map((data) => {     // Main array
    
     return(
    
     data.map((subData) => {        // inside array
      return(
        .... your code
        {subData.yourArrayElementName}
       )
    
     })
    
     )
    
    })
    

    【讨论】:

      【解决方案2】:

      我认为您混淆了数组和对象。

      您将 materials 定义为一个数组,但随后尝试通过对象名称访问它(例如 items.uses.use1 object-notation 与 items.uses[0] array-notation)

      目前,您有一个对象数组,但每个对象都有不同的键 use1 vs use2

      如果您需要更多帮助,请考虑将您的示例精简为您遇到的问题。

      【讨论】:

      • 嘿,感谢您的反馈,我已在“显示出现问题的部分”下对其进行了修剪。我希望它能简化事情
      猜你喜欢
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      相关资源
      最近更新 更多