【问题标题】:query not called when props are changed in Apollo client with react js使用 React js 在 Apollo 客户端中更改道具时未调用查询
【发布时间】:2018-05-19 21:58:58
【问题描述】:

当 props 在 redux 存储中更新时,我需要调用一个查询。但它不是那样发生的,在加载组件时只调用一次查询。

我的代码是这样的--

  const sampleData = compose(graphql(ITEMS_BY_CATEGORY, {name:"itemByCategory", options: (props) => ({variables: {_id:props.user._id,searchstring:"", start:0,limit:10, itemype:"created",categoryid:props.categoryid}})}))(samplePage)

export default connect(
  (state) => ({user: state.user.user,categoryid: state.item.categoryid}),
  {}
)(sampleData);

我在 props 更新后尝试了 refetch 方法。喜欢 - this.props.data.refetch()。但它也没有工作。

【问题讨论】:

    标签: reactjs apollo react-apollo apollo-client


    【解决方案1】:

    添加componentWillReceiveProps() 并在那里调用查询。每当组件接收到新的道具时,它就会运行。如果您需要使用查询结果重新运行render 函数,您可能需要调用forceUpdate();

    componentWillReceiveProps(nextProps) {
           const localThis = this;  
           this.client.query({
                query: MY_QUERY,
                variables: {myVar: theVar},
            }).then((result) => {
                localThis.setState({myStateVar: result.data.relevantData});
                localThis.forceUpdate();
            });
      }
    

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 2018-02-09
      • 2021-05-17
      • 2019-02-17
      • 2021-09-17
      • 2017-06-23
      • 2018-07-23
      • 2018-02-07
      • 2020-01-23
      相关资源
      最近更新 更多