简体   繁体   English

React-Native子组件不会在父组件内呈现信息

[英]React-Native the child component don't render the information within the parent component

I'm developing a React Native app that using a ScrollView. 我正在开发一个使用ScrollView的React Native应用程序。 I want to display an amount of items (A card with title and a child component). 我想显示一些项目(带有标题和子项的卡片)。

The problem comes when I have to render each item, while the parent renders ok, the child does not. 当我必须渲染每个项目时,问题就来了,而父母却可以,而孩子却没有。

I don't know where could be the issue, here's my code: 我不知道问题可能出在哪里,这是我的代码:

    import React, {Component} from 'react';
    import {View, Text} from 'react-native';

    const mismo =[
     'Mismo',
     'Mismo',
     'Mismo',
     'Mismo',
     'Mismo'
    ];


    class Mismo extends Component {


     renderMismo2(){
       mismo.map((item) =>{
       return(
         <View>
           <Text>{item}</Text>
         </View>
       )
     })
    }

  render(){
    return(
      <View>
      {this.renderMismo2()}
      </View>
    );
  }
}

export default Mismo;

================================= ================================

import React, {Component} from 'react';
import {View, Text, ScrollView} from 'react-native';
import {Card} from 'react-native-elements';

import PriceCard from '../components/PriceCard';
import Mismo from '../components/Mismo';


class OrderPricingCard extends Component{
  renderAllPrices(){
    this.props.data.orders.map((item, i) => {
      return(
        <View>
          <PriceCard
            key={item.transporterName}
            data={item}
          />
        </View>
      );
    })
  }

  renderMismo(){
    return(
      <Mismo />
    );
  }

  render () {
    return (
      <Card
        containerStyle={styles.cardContainer}
        title={`Pedido: ${this.props.data.id}`}
      >
        <ScrollView
          horizontal
        >
            {this.renderMismo()}

            {this.renderAllPrices()}



        </ScrollView>
      </Card>
    );
  }
}

const styles = {
  cardContainer:{
    borderRadius: 10,
    shadowColor: "#000",
    shadowOffset: {
        width: 0,
        height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
  }

}

export default OrderPricingCard;

This can be an easy mistake to make! 这是容易犯的错误! I've done it several times. 我已经做过几次了。 What's happened is you've forgotten the return statement for the render methods ( renderMismo2() and renderAllPrices() ) found in each component. 发生的事情是您忘记了在每个组件中找到的render方法( renderMismo2()renderAllPrices() )的return语句。 Although the map methods correctly have return statements, you're not actually returning anything from the functions themselves. 尽管map方法正确地具有return语句,但实际上您并未从函数本身返回任何内容。

If you were to console.log() either of those function calls above the return in the React render() method, you would see undefined in the console. 如果要在React render()方法的return上方使用console.log()任何一个函数调用,则在控制台中会看到undefined的内容。

Here's what they would look like corrected. 这是他们看起来更正的内容。

renderAllPrices(){
    // added the 'return' keyword to begin the line below
    return this.props.data.orders.map((item, i) => {
      return(
        <View>
          <PriceCard
            key={item.transporterName}
            data={item}
          />
        </View>
      );
    })
  }

 renderMismo2(){
   // same here, added the 'return' keyword
   return mismo.map((item) =>{
   return(
     <View>
       <Text>{item}</Text>
     </View>
   )
 })
}

I tested the above in a React Native sandbox and it works. 我在React Native沙箱中测试了以上内容,并且可以正常工作。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM