2017-08-26 40 views
0

如何顯示在組件以下減速器(myData)做:如何在地圖對象中呈現減速

const initialState = [   // <= STATE AS ARRAY 
    { 
    id: '01', 
    name: 'Name One', 
    number: 11 
    }, 
    { 
    id: '02', 
    name: 'Name Two', 
    number: 22 
    } 
]; 

const initialStateNew = { // <= STATE AS OBJECT 
1: { 
    id: '01', 
    name: 'Name One', 
    number: 11 
    }, 
2: { 
    id: '02', 
    name: 'Name Two', 
    number: 22 
    } 
} 

export default function myData (state = initialStateNew, action) { 
    switch (action.type) { 
    case "somthing": 
     return { 
     ...state 
     } 
    default: 
     return state 
    } 
} 

我已經combinedReducers這一個,人稱其爲myData和使用mapStateToPropsmapDispatchToProps到將減速器映射到組件中。

現在,下面的代碼工作使用initialState時:

const renData = this.props.myData.map((data, idx) => { 
    return (
    <View key={idx}> 
     <TouchableOpacity 
     onPress={() => Alert.alert("ID: ", data.id)} 
     > 
     <Text style={styles.welcome}>{data.id} - {data.name} - {data.number}</Text> 
     </TouchableOpacity> 
    </View> 
    ) 
}); 

代碼工作的原因,而是一個數組。如何在renData中映射對象initialStateNew{createUI}


EDIT1

this.props.newData從mapStateToProps

const createUI =() => { 
    let uiItems = []; 

    for(let i in this.props.newData) 
    uiItems.push(<Text key={data[i].id}> {data[i].name} </Text>) 

    return uiItems; 
} 

使用在View到來。收到錯誤:

enter image description here


EDIT2

const createUI =() => { 
    let uiItems = []; 

    for(let i in this.props.newData) 
    uiItems.push(
     <Text key={this.props.newData[i].id}> 
      {this.props.newData[i].name} 
     </Text> 
     ) 
    return uiItems; 
} 

const renData = this.createUI.map((data, idx) => { 
    return (
    <View key={idx}> 
     <TouchableOpacity 
     onPress={() => Alert.alert("ID: ", data.id)} 
     > 
     <Text style={styles.welcome}>{data.id} - {data.name} - {data.number}</Text> 
     </TouchableOpacity> 
    </View> 
) 
}); 
+0

[ReactJS中的對象循環]可能重複(https://stackoverflow.com/questions/45606360/loop-on-object-in-reactjs) –

+0

謝謝@MayankShukla。我已閱讀過,但有點令人困惑。你能告訴我,如果有另一種方式比'地圖'。除非需要,否則我不想使用它。我認爲地圖更多地用於數組,對嗎? – Somename

+0

yes'map'是數組,因此我們首先需要使用Object.keys或Object.values然後使用map。 –

回答

1

只要改變你renData這樣

const renData = Object.keys(this.props.myData || []).map((key, idx) => { 
    let data = this.props.myData[key] 
    return (
    <View key={idx}> 
     <TouchableOpacity 
     onPress={() => Alert.alert("ID: ", data.id)} 
     > 
     <Text style={styles.welcome}>{data.id} - {data.name} - {data.number}</Text> 
     </TouchableOpacity> 
    </View> 
    ) 
}); 

編輯:

Object.keys(this.props.myData)返回鍵陣列(如果this.props.myData是一個對象)或返回索引的陣列(即 [「0」,「1」,...])如果它是一個數組。

this.props.myData || []表示如果this.props.myData爲空或未定義,則僅使用一個空數組。這是爲了當this.props.myData爲空或未定義時它不會拋出不必要的錯誤。

+0

它的工作!謝謝。 「Object.keys(this.props.myData)」究竟做了什麼? – Somename

+0

爲什麼你添加'|| []'?這是否意味着它是一個數組還是空的? – Somename

+1

@Somename查看這些問題的編輯答案, –

1

它的Loop on Object in ReactJS重複,但OP不想使用map所以替代的解決方案。

從渲染方法中調用函數並在對象上使用for循環來創建ui項並返回結果。

像這樣:

_

createUI(){ 

    if(!Object.keys(data).length) return null; 

    let uiItems = []; 
    for(let i in data) 
     uiItems.push(
      <View key={data[i].id}> 
       <TouchableOpacity 
        onPress={() => Alert.alert("ID: ", data[i].id)} 
       > 
        <Text style={styles.welcome}>{data[i].id} - {data[i].name} - {data[i].number}</Text> 
       </TouchableOpacity> 
      </View> 
     ) 

    return uiItems; 
} 

render(){ 
    return(
     <div> 
      {this._createUI()} 
     </div> 
    ) 
} 

檢查這個片段:

const initialStateNew = { 
 
1: { 
 
    id: '01', 
 
    name: 'Name One', 
 
    number: 11 
 
    }, 
 
2: { 
 
    id: '02', 
 
    name: 'Name Two', 
 
    number: 22 
 
    } 
 
} 
 

 
for(let i in initialStateNew) 
 
    console.log('key, value = ', i, initialStateNew[i].name);

+0

感謝兄弟。我想我能夠理解這裏的登錄信息。所以首先從對象和渲染中創建數組,對嗎?數據來自哪裏?我試過這個,並得到一個錯誤。更新我的問題('Edit1')。請幫忙。 – Somename

+0

你是否從任何api獲取數據?把檢查'if(!Object.keys(data).length)返回null;'它將工作檢查更新的答案。 –

+0

您需要用您的數據變量替換數據,首先我們需要將數據轉換爲數組然後映射。 –