javascript - React, Apollo 2, GraphQL, Authentication. How to re-render component after login - Stack Overflow

I have this code: I don't understand:1) How to re-render() Menu ponent after:this.props.client.que

I have this code:

I don't understand:

1) How to re-render() Menu ponent after:

this.props.client.query({
  query: CURRENT_USER_QUERY,
  fetchPolicy: "network-only"
});

If I login() I expect my Menu ponent to re-render() itself. But nothing. Only if I click on the Home link it re-render() itself. I suspect because I'm using this to render it:

<Route ponent={Menu} />

for embrace it in react-router props. Is it wrong?

Plus, if inspect this.props of Menu after login() I see loading: true forever. Why?

2) How to prevent Menu ponent to query if not authenticated (eg: there isn't a token in localStorage); I'm using in Menu ponent this code:

export default graphql(CURRENT_USER_QUERY)(Menu);

3) Is this the right way to go?

I have this code: https://codesandbox.io/s/507w9qxrrl

I don't understand:

1) How to re-render() Menu ponent after:

this.props.client.query({
  query: CURRENT_USER_QUERY,
  fetchPolicy: "network-only"
});

If I login() I expect my Menu ponent to re-render() itself. But nothing. Only if I click on the Home link it re-render() itself. I suspect because I'm using this to render it:

<Route ponent={Menu} />

for embrace it in react-router props. Is it wrong?

Plus, if inspect this.props of Menu after login() I see loading: true forever. Why?

2) How to prevent Menu ponent to query if not authenticated (eg: there isn't a token in localStorage); I'm using in Menu ponent this code:

export default graphql(CURRENT_USER_QUERY)(Menu);

3) Is this the right way to go?

Share Improve this question edited Dec 9, 2017 at 19:50 Dane 9,5935 gold badges37 silver badges58 bronze badges asked Dec 5, 2017 at 13:58 user4412054user4412054 5
  • <Route ponent={Menu} /> I didn't seem to find this line of code.? – Dane Commented Dec 9, 2017 at 19:34
  • Is mented because I'm trying to use it without. – user4412054 Commented Dec 9, 2017 at 19:35
  • @Dane, if I remove that line and use just <Menu /> it doesn't re-render at all. – user4412054 Commented Dec 9, 2017 at 19:35
  • I reset the code as in question. – user4412054 Commented Dec 9, 2017 at 19:41
  • About (2), you can easily skip queries as explained here: apollographql./docs/react/basics/queries.html#graphql-skip – cfraser Commented Dec 13, 2017 at 16:31
Add a ment  | 

2 Answers 2

Reset to default 3 +100

First, let me answer your second question: You can skip an operation using the skip query option.

export default graphql(CURRENT_USER_QUERY, {
  skip: () => !localStorage.get("auth_token"),
})(Menu);

The problem now is how to re-render this ponent when the local storage changes. Usually react does not listen on the local storage to trigger a re-render, instead a re-render is done using one of this three methods:

  1. The ponent's state changes
  2. The parent of the ponent re-renders (usually with new props for the child)
  3. forceUpdate is called on the ponent

(Note that also a change of a subscribed context will trigger a re-render but we don't want to mess around with context ;-))

You might want to go with a bination of 2. and 3. or 1. and 2. In your case it can be enough to change the route (as you do in the login function). If this does not work you can call this.forceUpdate() on the App ponent after Login using a callback property on <Login onLogin={() => this.forceUpdate() } />.

EDITED

1) I just created new link https://codesandbox.io/s/0y3jl8znw

You want to fetch the user data in the ponentWillReceiveProps method:

  ponentWillReceiveProps() {
    this.props.client.query({query: CURRENT_USER_QUERY})
    .then((response) => {
      this.setState({ currentUser: response.data.currentUser})
    })
    .catch((e) => {
      console.log('there was an error ', e)
    })
  }

This will make the ponent re-render.

2) Now when we moved the query call in the ponent's lifecycle method we have full control over it. If you want to call the query only if you have something in localstorage you just need to wrap the query in a simple condition:

  ponentWillReceiveProps() {
    if(localstora.getItem('auth_token')) {
      this.props.client.query({query: CURRENT_USER_QUERY})
      .then((response) => {
        this.setState({ currentUser: response.data.currentUser})
      })
      .catch((e) => {
        console.log('there was an error ', e)
      })
    }
  }

3) You want to store the global application state in redux store. Otherwise you will have to fetch the user info every time you need to work with it. I would remend to define a state in you App ponent and store all the global values there.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745622074a4636579.html

相关推荐

  • SVN服务器安装 - Windows系统

    文章目录 前言1 下载安装1.1 下载安装包1.2 软件安装1.3 异常处理 2 仓库创建3 权限分配 前言 Windows系统下SVN服务器搭建及遇到相关问题和处理方法记录 1 下载安装 1.1 下载安装包 Windows系统中的S

    1小时前
    00
  • Windows下配置Golang开发环境,并安装配置GoLand IDE

    作者&#xff1a;非妃是公主 专栏&#xff1a;《Golang》 博客地址&#xff1a;https:blog.csdnmyf_666 个性签&#xff1a;顺境不惰&#xff0c;逆境不馁

    1小时前
    00
  • UnoCSS即时按需引用的原子css引擎

    什么是unocss?很多前端可能第一次听说这个词unocss是一个即时的原子CSS引擎,它可以让你用简短的类名来控制元素的样式,而不需要写复杂的CSS代码。当然,原子样式也有很多选择,最著名的就是 Tailwind。但由于Tailwind

    1小时前
    20
  • windows 配置 upx

    ​1、下载:2、解压:解压后​3、配置环境变量,右键我的电脑-——》属性——》高级属性:在Path中添加: D:ProgramFileupx-5.0.0-win644、验证cmd中输入:代码语言:txt复制upx --version

    1小时前
    20
  • 谷歌云第三方SSH工具登录与一键重装系统

    一&#xff0c;设置 root 密码 先选择从浏览器打开 ssh 连接服务器切换到 root 账号&#xff0c;输入代码&#xff1a;sudo -i设置 root 密码&#xff0c;输入代码&

    1小时前
    00
  • 流固耦合:基本概念、适用软件及 Abaqus 与 Powerflow 的协同仿真

    在工程和科学研究的诸多领域,流固耦合现象广泛存在且对系统性能有着关键影响。理解流固耦合的本质及其应用,对于优化设计、保障安全运行意义重大。同时,借助专业的流固耦合软件,能够更高效地对相关问题进行分析与模拟。接下来,让我们深入探究流固耦合的奥

    1小时前
    00
  • MySQL8.4 Enterprise安装Firewall及测试

    参考:.4enfirewall.html1.首先执行安装SQL,路径在baseshare目录下代码语言:javascript代码运行次数:0运行复制cd u01mysql3308baseshare[root@mysql8_3

    1小时前
    00
  • AI生态暗战升级,科技巨头铁幕下的终极博弈

    一场围绕AI与智能体的标准、协议及生态的暗战已然蓄势待发。在美剧《权力的游戏》中,不到终局,主角归属始终成谜。如今的AI行业,正上演着同样扣人心弦的戏码。这并非是传统意义上的军事或政治博弈,而是一场围绕AI与智能体的标准、协议及生态展开的暗

    1小时前
    00
  • Go项目实战

    上节课我给大家介绍了怎么给Go项目做单元测试的规划,当然这里仅限于跟咱们课程里的实战项目一样分层架构设计做的还可以的项目哦,要是所有逻辑都耦合在Controller里,那这个规划就不适用了。。。,所有逻辑都耦合在Controller里还做个

    58分钟前
    00
  • 计算机开机长鸣报警,电脑开机报警,详细教您电脑开机一直长鸣报警怎么办

    朋友们在使用电脑的过程中&#xff0c;经常会碰到开机一声长鸣&#xff0c;然后一直响个不停&#xff0c;这到底是怎么回事呢? 其实这个现象的产生一般是因为内存的问题&#xff0c;那用户遇到电脑开机一直

    57分钟前
    00
  • 电脑密码在哪里设置win11,win11电脑开机密码怎么设置

    Win11系统由于许多设置和以前系统不一样了&#xff0c;所以很多用户们操作非常不习惯&#xff0c;有很多的小伙伴不知道win11系统怎么设置开机密码。给电脑设置密码&#xff0c;只有自己能打开进入系统桌面&a

    45分钟前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    38分钟前
    00
  • 面试官:从三万英尺角度谈一下Ceph架构设计(1)

    把面试官当陪练,在找工作中才会越战越勇大家好我是小义同学,这是大厂面试拆解——项目实战系列的第3篇文章,如果有误,请指正。本文主要解决的一个问题,Ceph为例子 如何描述项目的架构。一句话描述:主要矛盾发生变化10年前的技术和方案,放到10

    36分钟前
    00
  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    30分钟前
    00
  • Prometheus配置docker采集器

    Prometheus 配置 Docker 采集器Prometheus 是一个开源的监控系统和时间序列数据库,广泛用于容器化环境中。通过监控 Docker 容器,用户可以实时获取服务性能、资源使用情况等信息。本文将介绍如何为 Docker 容

    22分钟前
    00
  • ascend pytorch 踩坑.

    在910b上安装pytorch 和 pytorch_npu, 因为后续准备装vllm, 所以torch_npu是特殊的版本.代码语言:shell复制pip install torch==2.5.1 --extra-index pip in

    20分钟前
    00
  • Power BI 无公式实现帕累托图表

    帕累托分析(Pareto Analysis),也被称为8020法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表)

    17分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    14分钟前
    00
  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因&#xff0c;重装了好几次电脑&#xff0c;因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复&#xff0c;在网上学会了怎么不卸载重装数据库&#xff0c;自己记录以备后面自己查

    12分钟前
    00
  • 深度学习在DOM解析中的应用:自动识别页面关键内容区块

    爬虫代理摘要本文介绍了如何在爬取东方财富吧()财经新闻时,利用深度学习模型对 DOM 树中的内容区块进行自动识别和过滤,并将新闻标题、时间、正文等关键信息分类存储。文章聚焦爬虫整体性能瓶颈,通过指标对比、优化策略、压测数据及改进结果,展示了

    9分钟前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信