3 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果您不想像其他答案所建議的那樣將其包裝在另一個(gè)div中,您也可以將其包裝在一個(gè)數(shù)組中,它將起作用。
// Wrong!return ( <Comp1 /> <Comp2 />)
它可以寫成:
// Correct!return ( [<Comp1 />, <Comp2 />])
請(qǐng)注意,以上內(nèi)容會(huì)產(chǎn)生警告: Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of 'YourComponent'.
這可以通過(guò)向key
組件添加屬性來(lái)修復(fù),如果手動(dòng)添加這些屬性,則添加如下:
return ( [<Comp1 key="0" />, <Comp2 key="1" />])
以下是有關(guān)鍵的更多信息:組合與繼承

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
對(duì)于Rect-Native開(kāi)發(fā)人員。我在FlatList中的renderingItem時(shí)遇到此錯(cuò)誤。我有兩個(gè)Text組件。我在下面使用它們
renderItem = { ({item}) => <Text style = {styles.item}>{item.key}</Text> <Text style = {styles.item}>{item.user}</Text>}
但在我把這些拖曳內(nèi)部視圖組件后,它對(duì)我有用。
renderItem = { ({item}) => <View style={styles.flatview}> <Text style = {styles.item}>{item.key}</Text> <Text style = {styles.item}>{item.user}</Text> </View> }
您可能正在使用其他組件,但將它們放入View可能適合您。

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
很簡(jiǎn)單,我們可以使用父元素div來(lái)包裝所有元素,或者我們可以使用高階組件(HOC)的概念,即對(duì)于反應(yīng)js應(yīng)用程序非常有用
render() { return ( <div> <div>foo</div> <div>bar</div> </div> );}
或者另一種最好的方法是HOC非常簡(jiǎn)單而不是很復(fù)雜只需在項(xiàng)目中添加一個(gè)文件hoc.js并簡(jiǎn)單地添加這些代碼
const aux = (props) => props.children;export default aux;
現(xiàn)在import hoc.js文件在你想要使用的地方,現(xiàn)在不是用div元素包裝,而是我們可以用hoc包裝。
import React, { Component } from 'react';import Hoc from '../../../hoc'; render() { return ( <Hoc> <div>foo</div> <div>bar</div> </Hoc> ); }
添加回答
舉報(bào)