小编api*_*art的帖子

innerHTML是异步的吗?

我希望我不会自欺欺人,但我想了解这两行代码中发生了什么:

document.body.innerHTML = 'something';
alert('something else');
Run Code Online (Sandbox Code Playgroud)

我观察到的是在HTML更新之前警报显示(或者它可能有但是页面没有刷新/重新绘制/无论如何)

检查这个codepen看看我的意思.

请注意,即使将alertsetTimeout(..., 0)没有帮助.看起来innerHTML实际更新页面需要更多的事件循环.

编辑:

我忘了提到我使用Chrome并没有检查其他浏览器.看起来它只在Chrome中可见.尽管如此,我仍然感兴趣为什么会发生这种情况.

html javascript dom google-chrome

98
推荐指数
3
解决办法
7029
查看次数

来自浏览器的带有正文的异步 GET 请求

好吧,我知道这是一个坏主意,不应该这样做,但为了这个问题,请假设没有其他方法 - 我得到的 API 端点需要以空对象作为主体的 GET 请求。

有没有办法从浏览器执行异步请求?我正在使用在幕后axios使用的库, MDN说当 HTTP 方法是 时会擦除主体。我尝试使用本机,但它在浏览器中给我这个错误: XMLHttpRequestsendGETfetchTypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

有什么办法可以做到吗?

javascript get xmlhttprequest fetch-api axios

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

如何在 Visual Studio Code 中关闭 Typescript 语言基础建议?

我发现 Typescript Language Basics 建议很烦人 - 它们总是优先于我的建议,我想将其关闭。有谁知道这是怎么做到的吗?

typescript visual-studio-code

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

如何使用打字稿扩展react-bootstrap组件?

我正在使用react-bootstrap@1.0.0-beta.14并尝试Button使用react-bootstraps创建自定义组件Button,添加另一个道具,isLoading. 由于react-bootstrap类型的定义方式,我最终导入了一些帮助器类型并复制了react-bootstrap类型的一部分:

import React from 'react'
import { Button as Btn, ButtonProps } from 'react-bootstrap'
import { BsPrefixProps, ReplaceProps } from 'react-bootstrap/helpers'

interface Props {
  isLoading?: boolean
}

type BtnProps<As extends React.ElementType = 'button'> = ReplaceProps<
  As,
  BsPrefixProps<As> & ButtonProps
>

export const Button: React.FC<BtnProps & Props> = ({ isLoading, disabled, ...props }) => {
  return <Btn {...props} disabled={isLoading || disabled} />
}
Run Code Online (Sandbox Code Playgroud)

几乎有效。我得到的错误告诉我refprop 类型是错误的:

属性“ref”的类型不兼容。

...

类型“(instance: …

typescript reactjs react-bootstrap

6
推荐指数
1
解决办法
2623
查看次数

打字稿:类型“字符串”在函数的返回值中不可分配给类型“从不”

基本上我感兴趣的是为什么这个例子有效:

interface Alpha {}
interface Beta {}

interface HelloMapping {
  'Alpha': Alpha
  'Beta': Beta  
}

const hello = <T extends keyof HelloMapping>(arg: T): HelloMapping[T] => 
{
  if (arg === 'Alpha') return {} as Alpha;
  return {} as Beta;
};

const a = hello('Alpha') // a is of interface Alpha
Run Code Online (Sandbox Code Playgroud)

但这个没有:

interface HelloMapping2 {
  'Alpha': string
  'Beta': number
}

const hello2 = <T extends keyof HelloMapping2>(arg: T): HelloMapping2[T] => 
{
  if (arg === 'Alpha') return "42" as string
  return 42 as …
Run Code Online (Sandbox Code Playgroud)

typescript

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

TypeScript:向命名函数添加接口(函数声明)?

刚开始使用 Typescript,并为我的函数使用接口,如下所示:

interface StringFunction {
    (arg1:string):string
}
Run Code Online (Sandbox Code Playgroud)

如果我将该接口声明为变量,则可以将该接口附加到函数:

let testFunction:StringFunction = function(arg1) {}
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚我将如何(或可以?)使用命名函数来做到这一点。这不起作用:

function testFunction:StringFunction(arg1) {}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

typescript

3
推荐指数
1
解决办法
1279
查看次数

酶试验中如何设置封固功能的类型?

我正在使用 Typescript 和 Enzyme 来测试反应组件。我对 Typescript 很陌生。

我在测试中有这个辅助函数:

const getComponent = (mountFn = shallow) => (
  mountFn(<Component />)
)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,这是有效的,getComponent()但一旦我这样做,getComponent(mount)它就会失败,因为打字稿假设getComponent返回ShallowWrapper。

我有一些问题:

  1. 我如何告诉 TypescriptmountFn可以是shallowor mount
  2. 我如何告诉它返回值可以是类型ShallowWrapperReactWrapper类型?
  3. 理想情况下 - 我如何告诉它返回值应该是ShallowWrapper何时shallow传递以及ReactWrapper何时mount传递的类型?
  4. 如何使用 @types/enzyme 中已定义的类型来执行此操作?
  5. 这是一个好的做法吗?在使用打字稿之前我一直这样做,但也许我应该创建两个单独的函数?

typescript reactjs enzyme

3
推荐指数
1
解决办法
4493
查看次数