简体   繁体   English

React-Native:无法从ListView发送道具

[英]React-Native:Not able to send props from ListView

I made a listView using redux and axios request and everything seems fine....but i can't get the object in my render method... 我使用redux和axios请求制作了一个listView,一切似乎都很好....但是我无法在我的render方法中获取对象...

Here is my code 这是我的代码

   import React from 'react';
import { View, ListView, ActivityIndicator } from 'react-native';
import { connect } from 'react-redux';
import { ItemOverView } from '../../components/common';
import * as actions from '../../actions';

class AwaitingScreen extends React.Component {
  static navigationOptions = {
  header: null,
  };

  state = { loading: true };

  componentWillMount() {
    this.props.getAwaitingOrders();
    this.createDataSource(this.props);
  }
  componentWillReceiveProps(nextProps) {
    this.setState({ loading: false });
    this.createDataSource(nextProps);
  }
  createDataSource(orders) {
    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2
    });
    this.dataSource = ds.cloneWithRows(orders);
  }

  renderListView() {
    if (this.state.loading) {
      return (<ActivityIndicator
        size='large'
        color='#397af8'
      />
    );
    }
    return (
      <ListView
        enableEmptySections
        dataSource={this.dataSource}
        renderRow={this.renderRow}
      />
    );
  }
  renderRow = (order) => {
    return (
      <ItemOverView
        item={order}
      />
    );
  }
  render() {
    return (
      <View>
        {this.renderListView()}
      </View>
    );
  }
}
const mapStateToProps = state => {
  return {
    awaitingOrders: state.orders.awaitingOrders,
    statusCodes: state.orders.statusCodes
  };
};
export default connect(mapStateToProps, actions)(AwaitingScreen);

ITEMVIEW: ITEMVIEW:

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

 class ItemOverView extends React.Component {
  render() {
    console.log(this.props.item);

    return (
      <View style={style.containerStyle}>
        <Text>{this.props.item.id}</Text>
      </View>
    );
  }
}
const style = {
  containerStyle: {
    borderWidth: 1,
    borderRadius: 2,
    borderColor: '#ddd',
    borderBottomWidth: 0,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.2,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 10,
  }
};
export { ItemOverView };

As i run this code,I get an error undefined is not an object (evaluating 'this.props.item.id') ...The json i receive has 6 items in it and I get it correctly(checked from console.log).If i pass something static to ItemView...say item = 'product' then it gets rendered 6 times...so everything seems to works well, But for some reason it won't get the object 当我运行这段代码时,我得到一个未定义的错误,不是一个对象(评估'this.props.item.id') ...我收到的json中有6项,我正确地获取了它(从console.log检查) )。如果我将静态内容传递给ItemView ... say item ='product',那么它将被渲染6次...所以一切似乎都正常运行,但是由于某种原因,它将无法获得对象

It seems you don't use propTypes. 似乎您没有使用propTypes。 In RN if you import your component from outside and send your data in to it, you can use propTypes. 在RN中,如果您从外部导入组件并将数据发送到其中,则可以使用propTypes。

You can install with npm. 您可以使用npm安装。

npm install --save prop-types

And after, here try this ITEMVIEW, it should work. 之后,在这里尝试此ITEMVIEW,它应该可以工作。 If not, this means problem is in your data. 如果不是,则意味着您的数据存在问题。 So, check your data before calling your component. 因此,在调用组件之前,请检查数据。

import React from 'react';
import PropTypes from 'prop-types';
import { View, Text } from 'react-native';

 class ItemOverView extends React.Component {

  constructor(props) {
      super(props);
  }

  render() {
    console.log(this.props.item);

    return (
      <View style={style.containerStyle}>
        <Text>{this.props.item.id}</Text>
      </View>
    );
  }
}
const style = {
  containerStyle: {
    borderWidth: 1,
    borderRadius: 2,
    borderColor: '#ddd',
    borderBottomWidth: 0,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.2,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 10,
  }
};
ItemOverView.propTypes = {
  item: PropTypes.object,
};
ItemOverView.defaultProps = {
  item: null,
};
export { ItemOverView };

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

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