我在这里关注 Apollo 的教程:https://github.com/howtographql/react-apollo/blob/master/src/components/Header.js .
在 Header.js
文件中,他们有:
const userId = localStorage.getItem(GC_USER_ID)
在我看来有点难看:每次调用 Header render()
时,你都会从 localStorage 读取数据?
使用 react-redux
我曾经使用这样的东西:
render() {
const { authentication } = this.props;
...
}
const mapStateToProps = state => ({
authentication: state.authentication
});
export default connect(mapStateToProps, { logoutAction })(Navbar);
此外,如果我在每个 render()
上从 localStorage 读取数据,并且假设我想将所有用户的数据存储在 localStorage 中(例如 {username: "john",age: "15"}
)当我每次重新渲染时都会将此 const
传递给我的子组件之一,因为 JSON.parse
使其成为一个新对象!每次!
如何使用 Apollo 拥有类似 Redux Store 的东西?
最佳答案
Apollo 管理状态的解决方案是通过 apollo-link-state。这是一些general info和 docs 。设置完成后,您可以在 GraphQL 查询中查询状态。
它尚未发布 1.0 版本,但它可用且相当容易设置。他们的例子还不是很好。但是,如果您对 GraphQL 解析器有一定的了解,那么设置起来相当简单。我正在用here成功了。
关于javascript - 访问 Apollo Store 就像我以前使用 Redux Store 一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631443/