小编Jak*_*ake的帖子

所有 React 组件 props 在 Typescript 中都应该始终是只读的吗?

看起来它们应该是这样,因为组件内的 props 永远不应该改变。我在 Google 或 Stack Overflow 上找不到任何答案,所以在这里提问只是为了确定一下。

typescript

4
推荐指数
1
解决办法
1114
查看次数

Jest + Material-UI:正确模拟 useMediaQuery

我在我的useMediaQuery()一个组件中使用 Material-UI 的函数来确定size<Button>在组件内使用的道具。

我正在尝试测试它在玩笑测试中是否按预期工作,但是我当前的实现不起作用:

describe("Unit: <Navbar> On xs screens", () => {

  // Incorrectly returns `matches` as `false` ****************************
  window.matchMedia = jest.fn().mockImplementation(
    query => {
      return {
        matches: true,
        media: query,
        onchange: null,
        addListener: jest.fn(),
        removeListener: jest.fn()
      };
    }
  );

  it("renders as snapshot", async () => {
    const width = theme.breakpoints.values.sm - 1;
    const height = Math.round((width * 9) / 16);
    Object.defineProperty(window, "innerWidth", {
      writable: true,
      configurable: true,
      value: width
    });
    const { asFragment …
Run Code Online (Sandbox Code Playgroud)

jestjs material-ui

4
推荐指数
1
解决办法
4154
查看次数

React-Testing-Library - 在 fireEvent 后拍摄快照

fireEvent.focus()我在调用后无法拍摄快照。

\n\n

这是测试。我这里有两个测试,1 比较输入聚焦之前的快照,1 比较输入聚焦之后的快照。

\n\n
describe("Unit: <OutlinedInput />", (): void => {\n\n  describe("Initial render", (): void => {\n    describe("renders as snapshot", (): void => {\n      it("for standard fields", (): void => {\n        const { asFragment } = render(<OutlinedInput {...standardProps} />, {});\n        expect(asFragment()).toMatchSnapshot();\n      });\n    });\n  });\n\n  describe("On focus in, no input", (): void => {\n    describe("renders as snapshot", (): void => {\n      it("for standard fields", (): void => {\n        const { getByLabelText, container, asFragment } = render(\n          <OutlinedInput {...standardProps} …
Run Code Online (Sandbox Code Playgroud)

jestjs react-testing-library

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

如何使用 Twilio 号码在 Zoiper 中发送/接收短信?

我有一个具有语音和短信功能的 Twilio 手机号码。我可以在 Zoiper 中拨打和接听电话,但我不知道如何设置它以便我也可以发送/接收短信。没有关于如何执行此操作的指南。我该如何设置?

voip sip twilio twilio-api twilio-functions

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

如何将车把文件复制到 dist 文件夹

我使用的是 Typescript,它从src/lib/文件夹进行编译,而 Babel,它从lib/到进行编译dist/

我遇到的问题是,当我编译应用程序时,.handlebars其中的文件src/email_templates/没有被复制到。dist/因此,当我运行该应用程序时,它会失败并出现错误,因为它dist/email_templates/不存在。

我正在使用nodemailer-handlebars包并尝试将email_templates/文件夹移动到项目根目录并设置viewPath为该文件夹,但是这也会失败,因为应用程序位于var/www/api.my-domain.com/. 似乎api.my-domain.com文件夹名称中有句点会导致 viewPath 失败并出现错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:125:11)
    at Object.resolve (path.js:1080:7)
    at ExpressHandlebars._resolveLayoutPath (/var/www/api.my-domain.com/node_modules/express-handlebars/lib/express-handlebars.js:342:17)
    at ExpressHandlebars.<anonymous> (/var/www/api.my-domain.com/node_modules/express-handlebars/lib/express-handlebars.js:223:35)
Run Code Online (Sandbox Code Playgroud)

这是我用于构建应用程序的 package.json 脚本:

"build": "tsc && babel ./lib --out-dir ./dist ",
Run Code Online (Sandbox Code Playgroud)

我如何更改它,以便删除当前内容,dist/email_templates然后将内容复制src/email_templatesdist/email_templates

node.js handlebars.js typescript babeljs npm-scripts

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

当类型定义中使用泛型和条件时,Typescript 不会在 switch 语句中推断类型

我有一个函数的类型定义。我将第二个参数的类型定义为以第一个参数的类型为条件,如下所示:

const FOO = "FOO";
const BAR = "BAR";

let fooPayload = {
    zip: "zap"
}
let barPayload = {
    cat: "dog"
}

type ActionTypes = typeof FOO | typeof BAR;
interface MyFunction<T extends ActionTypes = ActionTypes> {
  (
    action: {
      type: T;
      payload: T extends typeof FOO
        ? typeof fooPayload
        : typeof barPayload;
    }
  ): boolean;
}
Run Code Online (Sandbox Code Playgroud)

MyFunctioninterface 所引用的函数在其中包含一个 switch 语句,该语句基于 进行切换,action.type并根据情况对action.payload. 下面是一个例子:

const myFunction:MyFunction = (action) => {
    switch (action.type) {
        case FOO: …
Run Code Online (Sandbox Code Playgroud)

typescript

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

使用 airbnb 和 prettier 扩展的 ESLint 配置,用于使用 typescript、jest 和 react-hooks 的 react 项目

我对如何设置配置文件以及我应该使用哪些配置/插件感到非常困惑。

我有一个使用 Typescript、Jest 和 React 钩子的 React 项目。

我知道我需要安装:

  • eslint
  • prettier, eslint-config-prettier,eslint-plugin-prettier
  • eslint-plugin-import

至于Airbnb的配置,我不确定是否需要安装:

  • eslint-config-airbnb, eslint-plugin-react,eslint-plugin-jsx-a11y
  • eslint-config-airbnb-base

这些似乎都不支持Typescript,所以我似乎还需要安装:

  • @typescript-eslint/eslint-plugin, @typescript-eslint/parser

对于 Jest,我需要安装:

  • eslint-plugin-jest

我不确定 React 钩子。我是否需要在此处安装任何其他内容,或者其他软件包之一是否包含对它的支持?我看到我可以选择安装:

  • eslint-plugin-react-hooks

这是必须的吗?

现在,对于配置文件,我关心两个方面:extendsplugins.

我看到其中一些包可以用/recommended. 我应该使用这些吗?该extends部分应该是什么?我见过将其设置为的示例:

{
  "extends": ["airbnb-base", "plugin:prettier/recommended"]
}
Run Code Online (Sandbox Code Playgroud)

虽然我已经看到其他使用的例子:

{
  "extends": ["airbnb", "prettier"]
}
Run Code Online (Sandbox Code Playgroud)

另一个使用示例:

{
  "extends": [
    "airbnb",
    "plugin:prettier/recommended",
    "prettier/react"
  ]
}
Run Code Online (Sandbox Code Playgroud)

其他 Typescript、Jest 和 React Hooks 插件怎么样?例如,eslint-plugin-jest建议添加"plugin:jest/recommended"到扩展中。这会与其他任何人发生冲突吗?我看到我也可以添加"plugin:@typescript-eslint/recommended""prettier/@typescript-eslint" …

eslint eslint-config-airbnb prettier eslintrc

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