reactjs - How to re-fetch data in react-apollo with value from the another input component?
问题描述
I have next issue with react-apollo: there are two components and both are located in totally different places:
<Search />
- simple input which triggersclient.query()
onEnter with some keyword typed by user.<OfferList />
- react-apollo component + react-infinite-scroll:
<Query
query={OFFERS_QUERY}
variables={{ page: 1, keyword: 'SomeDefaultKeyword' }}
>
{({ error, data, loading, fetchMore, client }) => {
if (error) {
return <div>Something went wrong. {error.message}</div>;
}
if (loading) {
return <Loader />;
}
const { search: { metadata, offers }} = data;
return (
<InfiniteScroll
hasMore={metadata.hits > 0}
loader={<Loader />}
loadMore={pageNumber => {
fetchMore({
updateQuery: (prev, { fetchMoreResult } )=> {
if (!prev.fetchMoreResult) return prev;
return {
search: {
__typename: prev.search.__typename,
criteria: fetchMoreResult.search.criteria,
metadata: prev.search.metadata,
offers: [
...prev.search.offers,
...fetchMoreResult.search.offers,
],
},
};
},
variables: {
keyword: ?,
page: ?,
},
});
}}
useWindow={true}
>
<List>
{offers.map( offer => (
<ListItem key={offer.id}>
<OfferCard description={offer.name} />
</ListItem>
))}
</List>
</InfiniteScroll>
);
}}
</OffersQuery>
Questions:
- How can I make next request in the
<OfferList />
usingfetchMore
with new keyword provided by user in the<Search />
? - How would you suggest to reset page number when user typed new keyword (needs to be 1 in that case)?
- User see offers list and spinner when scrolling down, but when user click I need spinner without products to be shown. Do you have any ideas?
I was thinking if there is some mechanism that allows me to save value from input to the apollo store and can force update in <Query />
. In that case it solves the problem, but I didn't get how I can do that correct. I was trying to call client.writeQuery
, client.watchQuery
, client.writeData
as input onChage
callback, but looks like it doesn't execute force <Query />
re-render. If you have experience to work with such case would be nice if you can share some ideas to me.
解决方案
推荐阅读
- php - 第 1 行第 1 列的 XML 错误:文档为空
- javascript - 使用递归将嵌套对象重新格式化为字符串数组
- python - 如何在 Python 的文本文件中搜索某个单词?
- python - 在 TensorFlow 中面临有关 OwnedIteror 属性错误的错误
- npm - 当我尝试将镭包安装到 Reactjs 时,会发生此错误,我该怎么办?
- javascript - 无法在选择更改和代码性能问题 ReactJS 中控制事件
- c# - 匹配 excel 函数名称的正则表达式/将 excel 函数名称替换为另一个函数名称
- python-3.x - 如何将熊猫列中的变量范围与另一列相加
- javascript - iFrame 中的 Pannellum 突然停止工作
- c# - 如何在 LINQ 查询中命名中间实体列表?