我在查询时遇到问题,我正在尝试获取两个无线电输入,其中之一没有任何问题,但另一个反应测试库引发了错误: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) 我希望环境变量有不同的配置文件,并且能够在我的下一个项目中使用它们。我看到了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) 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)。
根据这个 …
我不太清楚能够在反应本机应用程序中全局处理错误的过程。如何捕获任何应用程序错误并据此做出响应?例如,显示错误屏幕和“回家”按钮,或者运行特定代码(例如重试请求),以便用户可以从此类错误中恢复。使用哪些方法来处理反应原生中的全局错误?
在ReactJs项目中,您可以使用.storybook/preview.js文件添加全局装饰器和参数。如何实现同样的行为@storybook/react-native?
我需要的是用 来包装我所有的故事,ThemeProvider但我发现的独特方式是用 来包装单个故事.addDecorator()。
我想根据一个变量调用不同的钩子...这个值来自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) 我试图通过在用户向下滚动时将背景从透明更改为灰色来对反应导航本机堆栈标题进行动画处理。我正在阅读文档,它建议使用 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
在 React 中,每次更改时我都可以location使用use-react-router执行代码,这使我可以访问history、location和match属性。我所做的是使用 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来实现相同的行为?
我试图检查导航状态是否发生变化并在发生变化时运行代码,为了实现这一点,我使用了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)
这种方法有一个问题,如果您尝试在外部使用此方法 …
我正在尝试在我的反应本机/打字稿应用程序中使用笑话和 Storybook 以及 Storyshots 插件进行快照测试,但是当我尝试运行简单测试时遇到一些问题。
\n根据故事书文档中的快照测试部分,您唯一需要做的就是创建一个storybook.test.js包含以下内容的文件:
import initStoryshots from \'@storybook/addon-storyshots\';\ninitStoryshots();\nRun 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) 我使用纱线工作区在 monorepo (Lerna) 中创建了一个应用程序。
\n该应用程序的架构如下:
\nmy-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\nRun Code Online (Sandbox Code Playgroud)\n问题是尝试在package2中使用package1jest时抛出以下错误时抛出以下错误,并且我还没有找到修复它的方法。
\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) reactjs ×7
react-native ×4
javascript ×3
jestjs ×3
storybook ×2
eslint ×1
next.js ×1
rollupjs ×1
typescript ×1