【问题标题】:React-Native the child component don't render the information within the parent componentReact-Native 子组件不会在父组件内呈现信息
【发布时间】:2018-10-11 14:46:04
【问题描述】:

我正在开发一个使用 ScrollView 的 React Native 应用程序。我想显示一定数量的项目(带有标题和子组件的卡片)。

当我必须渲染每个项目时,问题就出现了,而父级渲染正常,而子级没有。

我不知道问题出在哪里,这是我的代码:

    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;

【问题讨论】:

    标签: reactjs react-native native


    【解决方案1】:

    这可能是一个容易犯的错误!我已经做过好几次了。发生的事情是您忘记了每个组件中的渲染方法(renderMismo2()renderAllPrices())的 return 语句。尽管 map 方法正确地具有 return 语句,但您实际上并没有从函数本身返回任何内容。

    如果您要在 React render() 方法中在 return 上方调用这些函数中的任何一个 console.log(),您会在控制台中看到 undefined

    这是他们看起来更正后的样子。

    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>
       )
     })
    }
    

    我在 React Native 沙箱中测试了上述内容,并且可以正常工作。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-23
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多