我希望我不会自欺欺人,但我想了解这两行代码中发生了什么:
document.body.innerHTML = 'something';
alert('something else');
Run Code Online (Sandbox Code Playgroud)
我观察到的是在HTML更新之前警报显示(或者它可能有但是页面没有刷新/重新绘制/无论如何)
检查这个codepen看看我的意思.
请注意,即使将alert在setTimeout(..., 0)没有帮助.看起来innerHTML实际更新页面需要更多的事件循环.
编辑:
我忘了提到我使用Chrome并没有检查其他浏览器.看起来它只在Chrome中可见.尽管如此,我仍然感兴趣为什么会发生这种情况.
好吧,我知道这是一个坏主意,不应该这样做,但为了这个问题,请假设没有其他方法 - 我得到的 API 端点需要以空对象作为主体的 GET 请求。
有没有办法从浏览器执行异步请求?我正在使用在幕后axios使用的库, MDN说当 HTTP 方法是 时会擦除主体。我尝试使用本机,但它在浏览器中给我这个错误:
XMLHttpRequestsendGETfetchTypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
有什么办法可以做到吗?
我发现 Typescript Language Basics 建议很烦人 - 它们总是优先于我的建议,我想将其关闭。有谁知道这是怎么做到的吗?
我正在使用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: …
基本上我感兴趣的是为什么这个例子有效:
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,并为我的函数使用接口,如下所示:
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 和 Enzyme 来测试反应组件。我对 Typescript 很陌生。
我在测试中有这个辅助函数:
const getComponent = (mountFn = shallow) => (
mountFn(<Component />)
)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,这是有效的,getComponent()但一旦我这样做,getComponent(mount)它就会失败,因为打字稿假设getComponent返回ShallowWrapper。
我有一些问题:
mountFn可以是shallowor mount?ShallowWrapper或ReactWrapper类型?ShallowWrapper何时shallow传递以及ReactWrapper何时mount传递的类型?