作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 SSR 从服务器恢复初始状态的缓存
const cache = new InMemoryCache().restore(window.__APOLLO_STATE__)
经过几次突变,我需要将缓存硬重置为其初始状态。我天真地尝试将初始存储保持在组件状态并将其传递给 client.restore
componentDidMount () {
this.setState({
initState: this.props.client.extract()
})
}
// When I need to reset, I'd call this method...
handleCacheReset () {
this.props.client.restore( this.state.initState )
}
但是没有成功。由于我也在使用apollo-link-state,我想如果我将缓存数据保留为默认值,也许我可以在调用client.resetStore时写入它们
const cache = new InMemoryCache().restore(window.__APOLLO_STATE__)
const link = withClientState({ cache })
const client = new ApolloClient({
link, cache,
defaults: cache.data.data
})
client.onResetStore(link.writeDefaults)
// ...when it's time to reset the store
client.resetStore()
但我只收到 Missing field ... in {}
错误,并且没有数据写入存储。更糟糕的是,我无法使用 client.writeData
,因为我只有初始 window.__APOLLO_STATE__
。如果我可以读取整个缓存数据,那么这可能是合理的,但似乎没有 readData
方法。
到目前为止,我看到的唯一解决方法是重新创建整个 ApolloClient
并强制重新渲染树,这是昂贵且低效的。想法?
附注在 GitHub 上询问此问题,但他们没有任何答复就关闭了它。
最佳答案
这是在网络上还是在移动设备上?我自己无法解决这个问题,但在思考 apollo-cache-persist ,理论上您可以将缓存状态保存到本地存储,并在稍后将它们恢复到缓存中。
关于reactjs - 如何将缓存恢复到初始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737505/
我是一名优秀的程序员,十分优秀!