当前位置: 代码迷 >> 综合 >> React.Children.only expected to receive a single React element child
  详细解决方案

React.Children.only expected to receive a single React element child

热度:88   发布时间:2023-12-26 02:48:05.0

http://blog.csdn.net/meiyulong518/article/details/70748903

ReactNative使用TabBar报错react.children.only expected to receive a single react element child

实例代码:

  <TabBarIOS.Itemtitle="首页"icon={
   require('./img/动态副本.png')}selected={
   this.state.selectedTab === '首页'}onPress={
   () =>{this.setState({selectedTab:'首页',});}}></TabBarIOS.Item>//一运行就报错react.children.only expected to receive a single react element child

这里写图片描述

解决办法: 

React Native 中无论是 TabBarIOS.Item 还是 TabBar.Item 必须有只有一个组件,说白了就是需要有子组件的存在,并且只存在一个子组件。

<TabBarIOS.Item title="首页" icon={require('./img/动态副本.png')} selected={this.state.selectedTab === '首页'} onPress={() =>{ this.setState({ selectedTab:'首页', }); }}> <View > <Text>首页</Text> </View> </TabBarIOS.Item>



  <TabBarIOS.Item  title="首页"icon={require('./img/动态副本.png')}selected={this.state.selectedTab === '首页'}onPress={() =>{this.setState({selectedTab:'首页',});}}><View ><Text>首页</Text></View></TabBarIOS.Item>
  相关解决方案