小编Cri*_*rez的帖子

在 React 测试库中发现多个元素错误

我在查询时遇到问题,我正在尝试获取两个无线电输入,其中之一没有任何问题,但另一个反应测试库引发了错误:It Found multiple elements with the role "radio" and name /to/i:

查询

test('Render form with default items', () => {
  const handleUpdateValue = jest.fn();
  const handleNextStep = jest.fn();
  render(
    <Form
      handleNextStep={handleNextStep}
      updateValue={handleUpdateValue}
      transferFundsValues={{}}
    />
  );

  const amountInput = screen.getByLabelText(/amount/i);
  const fromRadio = screen.getByLabelText(/from/i);
  const toRadio = screen.getByLabelText(/to/i);
  const messageInput = screen.getByLabelText(/message/i);

  const continueButton = screen.getByRole('button', { name: /continue/i });
  const cancelButton = screen.getByRole('button', { name: /cancel/i });

  // If no value has been entered to the inputs, the continue …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs react-testing-library

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

如何在nextjs中使用不同的.env文件?

我希望环境变量有不同的配置文件,并且能够在我的下一个项目中使用它们。我看到了dotenv的例子。

但我不喜欢在 .env 文件中定义变量,也不想在 config.next.js 文件中定义它们。如果由于某种原因我将变量放在 .env 文件中但忘记将它们放在 config.next.js 文件中,则代码开始出现问题。有没有办法更有效地做到这一点?

我在 package.json 中的脚本:

"scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "lint": "eslint pages --ext .ts,.tsx,.js",
    "test": "jest",
    "commit": "git-cz",
    "dev:production": "dotenv next"
},
Run Code Online (Sandbox Code Playgroud)

我的 .env 变量

TITULO=react, typescript, material ui App
Run Code Online (Sandbox Code Playgroud)

成分

import { NextPage }          from 'next';
import { FunctionComponent } from 'react';

interface HelloWorldProps {
  nombre: string,
  saludo?: string
}


const HelloWorld: FunctionComponent<HelloWorldProps> = ({ nombre, saludo = 'noches' }: HelloWorldProps) => ( …
Run Code Online (Sandbox Code Playgroud)

environment-variables reactjs next.js

20
推荐指数
5
解决办法
2万
查看次数

Eslint:功能组件中的默认道具问题(Typescript - React)

我拥有的

import { NextPage } from 'next';
import React from 'react';

interface Props {
  name: string;
  gretting?: string; // Error: ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props) 
}

const Hello: React.FunctionComponent<Props> = ({ name, gretting = 'night' }: Props) =>
  <p>Hi {name} Good {gretting}</p>;

const Home: NextPage = () => <Hello name="Jhon Doe" />;

export default Home;
Run Code Online (Sandbox Code Playgroud)

问题

Eslint react 插件抱怨这个错误ESLint: propType "gretting" is not required, but has no corresponding defaultProps declaration.(react/require-default-props)

根据这个 …

typescript reactjs eslint

13
推荐指数
2
解决办法
7803
查看次数

如何在 React Native 中全局处理错误?

我不太清楚能够在反应本机应用程序中全局处理错误的过程。如何捕获任何应用程序错误并据此做出响应?例如,显示错误屏幕和“回家”按钮,或者运行特定代码(例如重试请求),以便用户可以从此类错误中恢复。使用哪些方法来处理反应原生中的全局错误?

error-handling reactjs react-native

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

如何在 Storybook 中添加全局装饰器

在ReactJs项目中,您可以使用.storybook/preview.js文件添加全局装饰器和参数。如何实现同样的行为@storybook/react-native

我需要的是用 来包装我所有的故事,ThemeProvider但我发现的独特方式是用 来包装单个故事.addDecorator()

storybook

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

React中有条件调用hooks的方法吗?

我想根据一个变量调用不同的钩子...这个值来自URL参数并且是动态的,但是钩子不能根据Hook的规则有条件地运行:

例子

// this hook comes from react-router dom to get the URL params
const param = const { productType, accountId, statementId } = useParams();

// I need to run a hook that calls a different API according productType value
if (productType === 'someType') {
  return useHookOne(accountId, statementId)
} else if (productType === 'otherType') {
  return useHookTwo(accountId, statementId)
} else {
  return useHookThree(accountId, statementId)
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

动画反应导航本机堆栈标题的问题

我想要的是

我试图通过在用户向下滚动时将背景从透明更改为灰色来对反应导航本机堆栈标题进行动画处理。我正在阅读文档,它建议使用 navigation.setOptions屏幕信息进行交互。

我正在使用react-native-reanimated来捕获滚动值并在用户与屏幕交互时更改它。

问题

我正在捕获滚动值并在setOptions方法内使用它,但它不起作用,它只是不执行更改。

import React from 'react';
import {
  useAnimatedScrollHandler,
  useSharedValue,
} from 'react-native-reanimated';

const MyScreen = ({ navigation }) => {
  const scrollY = useSharedValue(0);
  const scrollHandler = useAnimatedScrollHandler({
    onScroll: (e) => {
      scrollY.value = e.contentOffset.y;
    },
  });


  React.useLayoutEffect(() => {
    navigation.setOptions({
      headerStyle: {
        backgroundColor: scrollY.value > 0 ? 'black' : 'transparent',
      },
      headerTransparent: scrollY.value === 0,
    });
  }, [ navigation, scrollY.value ]);
}
Run Code Online (Sandbox Code Playgroud)

部门 …

react-native react-navigation react-native-reanimated react-native-reanimated-v2

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

有没有一种方法可以在每次路线/屏幕发生变化时通过反应本机导航来运行代码?

在 React 中,每次更改时我都可以location使用use-react-router执行代码,这使我可以访问historylocationmatch属性。我所做的是使用 auseEffect并在每次location属性更改时运行代码,如下所示:

import useRouter from "use-react-router";

const { location } = useRouter();

React.useEffect(() => {
  // run some code every time `location` change.
}, [location]);
Run Code Online (Sandbox Code Playgroud)

有没有办法通过react-native中的react-navigation来实现相同的行为?

更新 1 - 使用 useNavigationState 挂钩的临时解决方案

我试图检查导航状态是否发生变化并在发生变化时运行代码,为了实现这一点,我使用了useNavigationStatereact-navigation

import {useNavigationState} from '@react-navigation/native';

const navigationState = useNavigationState(state => state);

React.useEffect(() => {
  // run some code every time `location` change.
}, [navigationState]);
Run Code Online (Sandbox Code Playgroud)

这种方法有一个问题,如果您尝试在外部使用此方法 …

reactjs react-native react-native-navigation

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

在 React Native 中使用笑话进行故事书快照测试时出现问题

我拥有的

\n

我正在尝试在我的反应本机/打字稿应用程序中使用笑话和 Storybook 以及 Storyshots 插件进行快照测试,但是当我尝试运行简单测试时遇到一些问题。

\n

根据故事书文档中的快照测试部分,您唯一需要做的就是创建一个storybook.test.js包含以下内容的文件:

\n

故事书.test.js

\n
import initStoryshots from \'@storybook/addon-storyshots\';\ninitStoryshots();\n
Run Code Online (Sandbox Code Playgroud)\n

之后,一切都应该按预期工作,但控制台会抛出以下错误:

\n
\xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    This usually means that you are trying to\n import a file which Jest cannot parse, e.g.\nit\'s not plain JavaScript.\n\n    By default, if Jest sees a Babel config,\nit will use that to transform your files, ign\noring "node_modules".\n\n    Here\'s what you can do:\n     \xe2\x80\xa2 If you are trying …
Run Code Online (Sandbox Code Playgroud)

jestjs react-native storybook

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

使用 Rollup 和 Typescript 配置 Jest

我使用纱线工作区在 monorepo (Lerna) 中创建了一个应用程序。

\n

该应用程序的架构如下:

\n
my-monorepo\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 packages\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package1(shared components)\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package2(other package consuming the shared components)\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ./jest.config.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n
Run Code Online (Sandbox Code Playgroud)\n

问题

\n

问题是尝试在package2中使用package1jest时抛出以下错误时抛出以下错误,并且我还没有找到修复它的方法。

\n
\xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    This usually means that you are trying to import a file which Jest ca\nnnot parse, e.g. it\'s not plain JavaScript.\n\n    By default, if Jest sees a Babel config, it will use that to transfor\nm your …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs rollupjs

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