想要将数据传递给其他组件 - ListView

Want to pass data to other component - ListView

我添加并导入了 sample data。我想在 list view 中列出此文件中的数据,并将数据传递给 RenderRow 的行组件。但是出现错误

Row(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

import React, { Component } from 'react';
import { AppRegistry, View, ListView, Text, StyleSheet } from 'react-native';
import Row from './app/Row';
import data from './app/Data';

export default class ListViewDemo extends Component {
  constructor(props) {
  super(props);
  const rowHasChanged = (r1, r2) => r1 !== r2
  const ds = new ListView.DataSource({rowHasChanged});
  this.state = {
    dataSource: ds.cloneWithRows(data),
  };
  render() {
   return (
     <ListView
      dataSource={this.state.dataSource}
      renderRow={(data) => <Row {...data} />} // Getting error here
     />
   );
  }
}
AppRegistry.registerComponent('DemoApp',() => ListViewDemo)

这些是我的样本Data.js你可以查看data here.

export default data = [
   {...}, {...}
];

Row.js:

const Row = (props) => {
 <View Style={styles.container}>
  <Image source={{ uri: props.picture.large}} />
  <Text >
   {`${props.name.first} ${props.name.last}`}
  </Text>
 </View>
}

会是什么问题?

将此行更改为

renderRow={(data) => <Row {...data} />}

致此

 renderRow={(data) =><Row {...this.props} />}

这可能会帮助您在 Component

行中获取道具

ES6 only returns when there is no explicit blocks:

const cb = (param) => param * 2;

你应该明确地 return:

const Row = (props) => {
    return (
        <View Style={styles.container}>
            <Image source={{ uri: props.picture.large}} />
            <Text >
                {`${props.name.first} ${props.name.last}`}
            </Text>
        </View>
    );
}

检查这个 以获得进一步的解释。