【问题标题】:React Native - React Apollo - data prop undefinedReact Native - React Apollo - 数据道具未定义
【发布时间】:2018-11-05 01:53:45
【问题描述】:

我需要在组件的滚动视图中使用数据道具进行刷新。所以我需要在我的onRefresh 中调用data.refetch 进行复习。但我得到 data prop is undefined 错误。

代码:

class abc extends Component {
    render() {
      return (
        <View>
          <ScrollView
            onRefresh={this.props.data.refetch} // undefined error
            refreshing={this.props.data.loading} // undefined erro    
          >
          .....
           </ScrollView>
        </View>
      );
    }
}

export default compose(
 ....,
 graphql(
   MyQuery,{
      .....
   }
 )
)(abc);

为什么我的数据道具是未定义的?

【问题讨论】:

  • 没有。我没有用名字。我只需要刷新数据。无论如何,我已经通过将 props.data.refetch 映射到容器中的道具来解决这个问题。谢谢。

标签: react-native amazon-dynamodb react-apollo apollo-client aws-appsync


【解决方案1】:

我自己想通了。

class abc extends Component {
    static propTypes = {
      ....
      refetchData: PropTypes.func.isRequired,
      ....
    };

    render() {
      return (
        <ScrollView>
          <RefreshControl
            onRefresh={this.props.refetchData}
            refreshing={this.props.refreshing}
          />
        </ScrollView>
      );
    }
}

export default compose(
  graphql(MyQuery, {
    props: props => ({
      refetchData: props.data.refetch,
      refreshing: props.data.networkStatus === 4,
    })
  }),
);

我将 refetch 道具映射到自定义屏幕道具并从 RefreshControl 调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 2018-04-18
    • 1970-01-01
    • 2017-11-19
    • 2018-07-30
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多