2017-04-02 49 views
0

我试图显示一个包含数字的嵌套数组。该数组有6个元素(数组)。每个嵌套数组包含6个其他元素/数字。我试图在Square组件中显示每个数字。我有一个错误:this.props.inGrid.foreach不是一个函数。react-native - this.porps.inGrid不是函数

import React, { Component, PropTypes } from 'react'; 
import { View, StyleSheet } from 'react-native'; 
import Square from './Square'; 
import * as globalStyles from '../styles/global'; 


export default class Grid extends Component { 

    render() { 
    const row = []; 
    this.props.inGrid.foreach((r, i) => { 
     row.push(
     <Square key={i} sqValue={r[i]} /> 
    ); 
    }); 
    return (
     <View style={styles.grid}> 
     {row} 
     </View> 
    ); 
    } 

} 

Grid.propTypes = { 
    numbers: PropTypes.object 
}; 

const styles = StyleSheet.create({ 
    grid: { 
    backgroundColor: globalStyles.BG_COLOR, 
    flexDirection: 'row', 
    padding: 20, 
    justifyContent: 'center' 
    } 
}); 

下面是方形组件:

import React, { PropTypes } from 'react'; 
import { View, Text, StyleSheet } from 'react-native'; 
import * as globalStyles from '../styles/global'; 


const Square = ({ sqValue }) => { 
    return (
    <View style={styles.square}> 
     <Text>{sqValue}</Text> 
    </View> 
); 
}; 

Square.propTypes = { 
    sqValue: PropTypes.number 
}; 

const styles = StyleSheet.create({ 
    square: { 
    backgroundColor: globalStyles.BAR_COLOR, 
    width: 50, 
    height: 50, 
    borderWidth: 1, 
    borderStyle: 'solid', 
    borderColor: 'red' 
    } 
}); 
export default Square; 

我在做什么错?

+0

,它看起来像'inGrid'实际上不是的渲染功能是时间数组运行。我会去看一个组件,看看在第一次创建Grid组件时是否有数据不存在的可能性。例如,如果该数据来自api,并且在数据返回之前呈现网格。 编辑: 罢工所有......你打电话给'foreach'和函数应该叫'forEach' –

回答

1

看来,你打电话: this.props.inGrid.foreach 但基于错误的函数实际上是所谓forEach

+0

就是这样。谢谢。愚蠢的错误 – Wasteland