小编Seb*_*nes的帖子

如何使用 useReducer 钩子测试组件?

减速器

// src/reducers/FooReducer.js

export function FooReducer(state, action) {
  switch (action.type) {
    case 'update': {
      return action.newState;
    }
// ... other actions
    default:
      throw new Error('Unknown action type');
  }
}

Run Code Online (Sandbox Code Playgroud)

成分

// src/components/BarComponent.js

export function BarComponent() {
  const [state, dispatch] = useReducer(FooReducer, []);

  return (
    {state.map((item) => (<div />))}
  );
}
Run Code Online (Sandbox Code Playgroud)

测试

// src/components/BarComponent.test.js

it('should render as many divs as there are items', () => {
  act(() => {
    const { result } = renderHook(() => useReducer(FooReducer, [1]));
    const [, dispatch] …
Run Code Online (Sandbox Code Playgroud)

reactjs enzyme react-hooks react-hooks-testing-library

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

如何修复 eslint/prettier“解析错误:';' .css 文件的预期”?

这是我收到的错误示例,但我不知道如何修复:

// example.css

body {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

运行 eslint 时,这将在终端中返回以下错误:

1:5 错误 解析错误:';' 预期的

这是我的 eslintrc 和 prettierrc 配置,谢谢您的帮助:

// eslintrc.js

module.exports = {
    env: {
        browser: true,
        jest: true,
        node: true,
        es6: true,
    },
    parser: '@typescript-eslint/parser',
    plugins: ['@typescript-eslint'],
    extends: [
        'airbnb',
        'prettier',
        'prettier/react',
        'prettier/@typescript-eslint',
        'plugin:@typescript-eslint/recommended',
    ],
    parserOptions: {
        ecmaVersion: 2018,
        sourceType: 'module',
        ecmaFeatures: {
            jsx: true,
        },
    },
    rules: {
        'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
        'react/jsx-filename-extension': ['warn', { extensions: ['.tsx', '.jsx'] }],
        '@typescript-eslint/interface-name-prefix': ['error', { prefixWithI: 'always' …
Run Code Online (Sandbox Code Playgroud)

css eslint prettier

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

更改语言时如何翻译位置 URL?

我目前在网站的本地化过程中遇到了障碍。使用 i18next,我们的所有路由都会被翻译,并且默认语言会从 URL 中删除区域设置路径。

换句话说:
/accueil -> /en/home
/produits -> /en/products
等等...

我的问题是,当我更改语言时,网址不跟随(这是预期的,因为 i18next 不直接与反应路由器对话)。

i18下一个配置:

i18n
  .use(detector)
  .use(initReactI18next)
  .init({
    whitelist: ['fr', 'en'],
    fallbackLng: 'fr',
    defaultNS: 'common',

    detection: {
      lookupFromPathIndex: 0,
      checkWhitelist: true
    },

    interpolation: {
      escapeValue: false
    },

    resources: {
      en: {
        routes: enRoutes,
        [...]
      },
      fr: {
        routes: frRoutes,
        [...]
      }
    }
  });

Run Code Online (Sandbox Code Playgroud)

fr/routes.json:

{
  "home": "/accueil",
  "products": "/produits",
  [...]
}
Run Code Online (Sandbox Code Playgroud)

en/routes.json:

{
  "home": "/en/home",
  "products": "en/products",
  [...]
}
Run Code Online (Sandbox Code Playgroud)

app.jsx 中的路由器部分:

<Router forceRefresh>
  <Switch>
    <Route path={`/:locale(en|fr)?${t('routes:home')}`} component={HomeComponent} /> …
Run Code Online (Sandbox Code Playgroud)

localization internationalization i18next react-router react-i18next

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

如何以 Numpydoc 格式记录多个返回值?

我正在尝试使用 numpy 文档字符串格式记录元组返回值,但无法使其与 pycharm 类型提示一起使用。

我尝试了多种方法,甚至找到了一种适用于该类型的方法,但不允许我为其每个元素添加描述。

要记录的函数示例:

def function():
    foo = 42
    bar = {
        example : 1337,
        dictionary : 46,
    }
    return foo, bar
Run Code Online (Sandbox Code Playgroud)

现在,我可以记录它的一种方法是:

def function():
    """
    This is the function summary.

    Returns
    -------
    foobar : tuple[int,[dict[string, int]]
        This is a description of the return type
    """
    foo = 42
    bar = {
        'example' : 1337,
        'dictionary' : 46,
    }
    return foo, bar
Run Code Online (Sandbox Code Playgroud)

这将为我提供描述和正确的返回类型提示,但不是每个元素的单独描述,这是我想要的。

这是我想要实现的目标的一个非工作示例:

def function():
    """
    This is the function summary.

    Returns
    -------
    foo : int …
Run Code Online (Sandbox Code Playgroud)

python tuples type-hinting pycharm numpydoc

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