小编Igo*_*nko的帖子

无法在已卸载的组件上执行React状态更新

问题

我正在React中编写一个应用程序,无法避免出现一个超级常见的陷阱,该陷阱正在调用setState(...)after componentWillUnmount(...)

我非常仔细地查看了我的代码,并尝试放置一些保护子句,但是问题仍然存在,并且我仍在观察警告。

因此,我有两个问题:

  1. 我如何从堆栈跟踪中找出哪个特定的组件和事件处理程序或生命周期挂钩负责违反规则?
  2. 好吧,如何解决问题本身,因为我的代码在编写时就考虑到了这种陷阱,并且已经在试图防止这种情况发生,但是某些底层组件仍在生成警告。

浏览器控制台

Warning: Can't perform a React state update on an unmounted component.
This is a no-op, but it indicates a memory leak in your application.
To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount
method.
    in TextLayerInternal (created by Context.Consumer)
    in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep …
Run Code Online (Sandbox Code Playgroud)

javascript setstate typescript lodash reactjs

28
推荐指数
11
解决办法
3万
查看次数

如何将防御性编程技术结合在一起?

我想问你的问题很广泛但同时它非常具体.首先,我必须说,我最感兴趣的是适用于.net环境的答案.

好吧,我想提高我生成的代码级别.现在我主要使用TDD和静态代码分析来确保我的代码是正确的.最近我听了Dino Esposito关于代码合同的演讲,现在我想将它与其他技术结合使用.在听Dino的同时我也回忆起了Debug.Assert()Trace.Assert().

具体来说,我会问几个问题:

  • 我应该如何编写合同和单元测试以相互补充?
  • 我应该只在每种方法或公共方法中使用代码合同吗?
  • 我应该阻止使用Debug.Assert()吗?什么时候可以使用它们?(例如,请注意.net中的不变量仅在公共方法/属性出口处进行检查.那么,通过简单方法在方法中间进行一些检查是否可以Assert()?)
  • 你能否推荐我一个开源项目,所有这些技术都被正确使用,因为一张图片描绘了千言万语?

c# defensive-programming assert code-contracts fail-fast

21
推荐指数
1
解决办法
1133
查看次数

Google云端功能是否受到DDoS攻击保护?

据我了解,我的Google云功能可以全球访问.如果我想控制对它们的访问,我需要将授权作为函数本身的一部分来实现.说,我可以使用基于承载令牌的方法.这将保护此功能背后的资源免受未经授权的访问.

但是,由于该功能在全球范围内可用,它仍然可以由坏人进行DDoS编辑.如果攻击不如谷歌的防御强,我的功能/服务可能仍然是响应.这很好.但是,我不想支付我未授权访问该功能的一方所做的功能调用.(因为计费是每个函数调用的数量).这就是为什么在我负责收费之前了解Google云端功能是否检测到DDoS攻击以及启用反措施对我来说非常重要.

security google-cloud-platform google-cloud-functions

21
推荐指数
4
解决办法
3586
查看次数

ASP.NET Core 2.1中的UseStaticFiles,UseSpaStaticFiles和UseSpa有什么区别?

ASP.NET Core 2.1.1为appBuilder提供了一些看似相关的扩展方法:

  • UseStaticFilesMicrosoft.AspNetCore.StaticFiles
  • UseSpaStaticFilesMicrosoft.AspNetCore.SpaServices.Extensions
  • UseSpaMicrosoft.AspNetCore.SpaServices.Extensions

请帮助我理解他们的目的和彼此之间的关系吗?

另外,如果我以不同的顺序(例如app.UseStaticFiles() -> app.UseSpaStaticFiles() -> app.UseSpa()vs app.UseSpa() -> app.UseSpaStaticFiles() -> app.UseStaticFiles())运行这些方法,那么从服务器执行的角度来看有什么不同吗?

c# asp.net-core asp.net-core-2.0 asp.net-core-2.1

20
推荐指数
3
解决办法
3643
查看次数

Jest 的 `it.each()` 描述用于在引用 $predicate 时呈现箭头函数源代码

问题定义

\n\n

Jest 允许通过it.each前缀变量在测试用例的数据中使用。name$

\n\n

下面的代码产生如下输出:

\n\n
 PASS  src/array-functions/find-pairwise.spec.ts\n  findPairwise\n    \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (7ms)\n    \xe2\x88\x9a should return [1, 2] for [1, 2, 3] and [Function anonymous] (1ms)\n    \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous]\n    \xe2\x88\x9a should return [2, 3] for [1, 2, 3] and [Function anonymous] (1ms)\n    \xe2\x88\x9a should return [undefined, undefined] for [1, 2, 3] and [Function anonymous]\n\nTest Suites: 1 passed, …
Run Code Online (Sandbox Code Playgroud)

javascript testing unit-testing typescript jestjs

15
推荐指数
1
解决办法
5万
查看次数

Flutter 中的安全性:google_maps_flutter 需要 API 密钥,但如何安全地提供它们?

这个问题怎么重复呢?

\n

虽然 @MrUpsidedown 在评论部分提供了一些相关问题,但他们都没有真正回答我在这里试图强调的部分:如何安全地提供 API 密钥?

\n

在阅读了链接问题中的所有答案后,我倾向于自己写一个答案。

\n

语境

\n

我是一名非常新的 Flutter 开发人员,尝试将 Google 地图集成到演示应用程序中。

\n

为了节省时间,我决定尝试我能找到的最流行的 Google 地图 Widget 库,即google_maps_flutter. 该库的简洁文档显示了 Android 和 iOS 使用示例,并且都说明了 API 密钥是内联提供的,作为代码库的一部分

\n
import UIKit\nimport Flutter\nimport GoogleMaps\n\n@UIApplicationMain\n@objc class AppDelegate: FlutterAppDelegate {\n  override func application(\n    _ application: UIApplication,\n    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?\n  ) -> Bool {\n    GMSServices.provideAPIKey("YOUR KEY HERE") // <------------------------------------  O--\xd0\xbf\xd0\xbf\n    GeneratedPluginRegistrant.register(with: self)\n    return super.application(application, didFinishLaunchingWithOptions: launchOptions)\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
<manifest ...\n  <application ...\n …
Run Code Online (Sandbox Code Playgroud)

google-maps api-key dart flutter google-maps-flutter

15
推荐指数
1
解决办法
4114
查看次数

在Windows上与Gulp进行E2E测试编排:无法杀死进程

我想要实现的目标

这个问题与我最近以一个可怕的hack™ 关闭的另一个问题有关.

我正在尝试编写一个脚本,可以在CI /构建管道的上下文中使用一个步骤.

该脚本应该为我们的Angular单页面应用程序(SPA)运行基于Protractor的端到端测试.

该脚本需要执行以下操作(按顺序):

  1. 运行名为"App"的.NET Core微服务
  2. 运行名为"Web"的.NET Core微服务
  3. 运行SPA
  4. 运行一个执行Protractor测试的命令
  5. 在步骤4完成后(成功或出错),终止在步骤1-3中创建的进程.这绝对必要的,否则构建将永远不会在CI中完成和/或将会出现僵尸Web/App/SPA进程,这将破坏未来的构建管道执行.

问题

我还没有开始处理第4步("e2e测试"),因为我真的想确保第5步("清理")按预期工作.

正如你猜的那样(右),清理步骤不起作用.具体来说,进程"App"和"Web"不会因某种原因而被杀死并继续运行.

顺便说一句,我确保我的gulp脚本以提升(管理员)权限执行.

问题 - 更新1

我刚刚发现问题的直接原因(我认为),我不知道究竟是什么原因.按照我的预期,有5个进程而不是1个进程.例如,对于App流程,在流程管理器中观察到以下流程:

{                          
  "id": 14840,             
  "binary": "cmd.exe",     
  "title": "Console"       
},                         
{                          
  "id": 12600,             
  "binary": "dotnet.exe",  
  "title": "Console"       
},                         
{                          
  "id": 12976,             
  "binary": "cmd.exe",     
  "title": "Console"       
},                         
{                          
  "id": 5492,              
  "binary": "cmd.exe",     
  "title": "Console"       
},                         
{                          
  "id": 2636,              
  "binary": "App.exe",
  "title": "Console"       
}                          
Run Code Online (Sandbox Code Playgroud)

同样,为Web服务创建了五个进程而不是一个进程:

{ …
Run Code Online (Sandbox Code Playgroud)

javascript windows process node.js gulp

13
推荐指数
1
解决办法
268
查看次数

警告 NETSDK1080:面向 .NET Core 3.0 或更高版本时,不需要对 Microsoft.AspNetCore.App 的 PackageReference

如何修复在通过命令行运行 .NET Core 测试时收到的令人讨厌的警告dotnet test

dotnet --version返回3.1.101

$ dotnet test
watch : Started
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
    warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
    .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, the shared framework will be referenced automatically.
    Otherwise, the PackageReference should be replaced with a FrameworkReference.
    [C:\github\demo\Demo\SmartHome.API\SmartHome.API.csproj]
C:\Program Files\dotnet\sdk\3.1.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(151,5):
    warning NETSDK1080: A PackageReference to Microsoft.AspNetCore.App is not necessary when targeting
    .NET Core 3.0 or higher. If Microsoft.NET.Sdk.Web is used, …
Run Code Online (Sandbox Code Playgroud)

.net c# warnings .net-core asp.net-core

12
推荐指数
1
解决办法
6851
查看次数

Async Pipe与Select/Options&ngModel/ngModelChange一起使用

问题描述

我正在尝试使用和/ 使用可观察字段进行基于select/ option的下拉工作.我的代码出了点问题,因为在运行时控制台会输出一个(请参见下图).asyncPipe[ngModel](ngModelChange)[object Object]

令我困惑的是,如果我使用[value]="payPeriod.id"它,它工作正常,数字id成功接收setSelectedPayPeriod(...).

component.html

  <select [ngModel]="selectedPayPeriod | json" (ngModelChange)="setSelectedPayPeriod($event)">
    <option *ngFor="let payPeriod of (payPeriodList | async)" [value]="payPeriod">{{ payPeriod.endDate }}</option>
  </select>
Run Code Online (Sandbox Code Playgroud)

component.ts

get payPeriodList(): Observable<PayPeriod[]> {
  return this._contextService.payPeriodList;
}

get selectedPayPeriod(): Observable<PayPeriod> {
  return this._contextService.selectedPayPeriod;
}

setSelectedPayPeriod(newValue: PayPeriod): void {
  console.warn(newValue);
  this._contextService.setSelectedPayPeriod(newValue);
}
Run Code Online (Sandbox Code Playgroud)

控制台输出

在此输入图像描述

歉意

对不起,我对plunker不是很熟悉,也无法快速找到我可以解决的Angular 2模板.

UPD.接受的解决方案 - 由AJT_82提供

  1. 使用[ngValue],而不是[value]option元素.
  2. 使用[ngModel]="selectedPayPeriod | async",而不是[ngModel]="selectedPayPeriod | json" …

angular

8
推荐指数
1
解决办法
9118
查看次数

正则表达式字符按照 grep 重复 n 次或更多次

我需要找到正则表达式来找到一个用 grep 重复 4 次或更多次的字符。

我知道表达式是 {n,},所以如果我需要查找行,例如,当字符“g”重复 4 次或更多次时,理论上 grep 手册页是:

grep "g{4,}" textsamplefile
Run Code Online (Sandbox Code Playgroud)

但不起作用。有什么帮助吗?

字符可以有其他字母。例如,有效匹配是:

g示例g of g有效g匹配g

g其他g有效g匹配gg这里g

GGGG其他

regex grep repeat

8
推荐指数
1
解决办法
1万
查看次数