- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我为以下组件创建故事,我只会收到几条红框内的消息,其中不包含任何我可以从中获得任何见解的信息:
import React, { useState, useContext } from "react";
import { FormGroup, FormControl, Col } from "react-bootstrap";
import Button from "../button/Button";
import AuthContext from "../../AuthContext";
import { useHistory } from "react-router-dom";
import LoginWarning from "./LoginWarning";
export function Login(props) {
const [email, setEmail2] = useState("");
const [password, setPassword] = useState("");
const [showAlert, setShowAlert] = useState(false);
const {
setRole,
setName,
setEmail,
setSessionId,
setLocalStorage
} = useContext(AuthContext);
let history = useHistory();
function validateForm() {
return email.length > 0 && password.length > 0;
}
const loginPost = {
email: email,
password: password
};
function handleSubmit(event) {
event.preventDefault();
const fetchUserInfo = async () => {
const result = await fetch(`/account`, {
method: "GET"
});
const body = await result.json();
console.log(body);
setRole(body.role);
setName(body.name);
setEmail(body.email);
history.push("/home");
};
const fetchAuth = async () => {
const result = await fetch(`/login`, {
method: "POST",
body: JSON.stringify(loginPost),
headers: {
"Content-Type": "application/json"
}
});
const body = await result.json();
console.log(body);
if (body.isUser === true) {
setSessionId(body.sessionId);
setLocalStorage(body.sessionId);
fetchUserInfo();
} else {
setShowAlert(true);
}
};
fetchAuth();
}
return (
<div>
{showAlert ? <LoginWarning /> : null}
<form onSubmit={handleSubmit}>
<Col lg={6}>
<FormGroup controlId="email" bsSize="large">
<label>Email</label>
<FormControl
autoFocus
type="email"
value={email}
onChange={e => setEmail2(e.target.value)}
/>
</FormGroup>
</Col>
<Col lg={6}>
<FormGroup controlId="password" bsSize="large">
<label>Passwort</label>
<FormControl
value={password}
onChange={e => setPassword(e.target.value)}
type="password"
/>
</FormGroup>
<Button disabled={!validateForm()} type="submit" text="Login" />
</Col>
</form>
</div>
);
}
export default Login;
import React from "react";
import { Login } from "./Login";
import { storiesOf } from "@storybook/react";
export default {
title: "Login Form"
};
storiesOf("Login Form", module).add("default", () => <Login />);
useHistory@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:100829:10
Login@http://localhost:6006/main.e31f087434cfd38286ae.bundle.js:763:84
renderWithHooks@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:84742:27
mountIndeterminateComponent@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:87276:13
beginWork$1@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:88638:16
callCallback@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:68837:14
invokeGuardedCallbackDev@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:68886:16
invokeGuardedCallback@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:68941:31
beginWork$$1@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:94239:28
performUnitOfWork@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:93166:12
workLoopSync@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:93139:22
performSyncWorkOnRoot@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:92738:11
scheduleUpdateOnFiber@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:92166:28
updateContainer@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:95562:15
legacyRenderSubtreeIntoContainer/<@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:95986:22
unbatchedUpdates@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:92901:12
legacyRenderSubtreeIntoContainer@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:95985:21
render@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:96073:12
render/<@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:20472:26
render@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:20471:10
_callee$@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:20563:20
tryCatch@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:19561:40
invoke@http://localhost:6006/vendors~main.e31f087434cfd38286ae.bundle.js:19787:30
defineIteratorMethods/
The above error occurred in the <Login> component:
in Login (at Login.stories.js:10)
in AuthProvider (at Login.stories.js:10)
in storyFn
in ErrorBoundary
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary. react-dom.development.js:21810
React 18
render render.js:70
render render.js:69
_callee$ render.js:161
Babel 8
_callee$ start.js:408
Babel 8
renderUI start.js:453
emit index.js:180
setSelection story_store.js:325
TypeError: "useContext(...) is undefined"
useHistory react-router.js:706
Login Login.js:19
React 16
render render.js:70
render render.js:69
_callee$ render.js:161
Babel 8
_callee$ start.js:408
Babel 8
renderUI start.js:453
emit index.js:180
setSelection story_store.js:325
最佳答案
如果您的组件使用 useContext()
,您实际上必须初始化上下文。 Storybook 与您的应用程序具有不同的入口点,因此除非您自己进行,否则所有正常的应用程序初始化都不会发生。
最简单的方法是使用装饰器(参见 https://storybook.js.org/docs/addons/introduction/)。
要在全局范围内执行此操作,请将类似的内容添加到您的 .storybook/config.js
中。 :
const StorybookWrapper = (storyFn) => (
<YourContextProvider>
{storyFn()}
</YourContextProvider>
);
addDecorator(StorybookWrapper);
关于reactjs - 故事书不呈现故事,错误 : "useContext(...) is undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881341/
最近进入一个项目,在react中的useContext看到了以前没见过的东西。 项目中的全局状态使用它发送到上下文的钩子(Hook),然后当稍后在全局状态上调用该钩子(Hook)时,全局状态是可访问的
我使用了一个结构 React Hooks .它基于全局 Context包含 reducer 的组合(如在 Redux 中)。 另外,我广泛使用custom hooks分离逻辑。 我有一个包含异步 AP
在 React 18.1.0 和 React-Dom 18.1.0 中使用 @mui/material 时,我遇到了情感和 react 之间的一系列兼容性问题。这很奇怪,因为我实际上安装了最新版本的
我试图弄清楚如何正确使用 React Context。我对尝试从函数组件外部访问 Context 的问题感到困惑。我收到错误: Line 9:18: React Hook "useContext" i
努力学习如何使用 react 钩子(Hook) .我用 创建上下文 我想将状态和功能一起转移到其他组件,但我需要有关如何执行此操作的帮助。我也想问这样做是否有意义。 事实上,我可以使用 来更新状态设置
我有两个组件 Parent 和 Child,我想将上下文从 Parent 导出到 Child,但这会导致循环依赖。 例如,考虑 Parent.js 是 import {Child} from './C
我对 React 非常着迷,但我遇到了一个关于 useContext 的大问题。真正了解它的内部工作原理是相当令人困惑的。就像它神奇地工作一样。 所以我的主模块中有这样的: export const
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
更新配置文件时,profile.name 会通过导航链接 (Navigation.js) 中的 useContext() 显示。但是,一旦页面刷新或注销期间,profile.name 就会被清除,pr
我试图以最简单的方式设置 useContext,但它一直返回 undefined。 我已经检查了好几次,但仍然无法破译我遗漏的内容。 上下文.js: import React from 'react'
我遇到了一个非常令人沮丧的问题,我不知道发生了什么。我有一个简单的上下文,如下所示: import React, { useState, createContext } from "react"; e
我正在尝试掌握 React 中新的 useContext 函数。在无状态功能组件中效果很好。例如: import React from 'react'; import LocaleContext fr
如果我在 Comp1.js const Comp1 = () => { const globalTheme = new createContext() return (
我无法弄清楚为什么我的 useContext 没有在这个函数中被调用: import { useContext } from "react"; import { MyContext } from ".
我在子组件中有一个按钮 AddMore.js它在列表中添加了更多项目,但它不会重新呈现呈现该列表的其他子组件 Pig.js .我认为我的设置会起作用,因为我使用 NextJS 框架以类似的方式设置了上
你好 我正在尝试进行菜单切换,其中我有一个以 false 作为初始值的变量,使用 react createContext 和 useContext Hook ,我将初始状态设置为 true // us
我有问题,我已经设置了 useContext与 useState提供程序中的值,但是当我导入它们并尝试更改值时,它会在本地更改而不是全局更改! 登录.js import { UserContext }
我正在尝试使用 React Context 来管理我的项目的状态,但这次我似乎无法让它适用于我的 CRA 代码库,而我在其他项目上进行了成功的尝试。 useContext 返回未定义,因此我无法访问上
我是 useContext 的新手钩子(Hook)并试图找出我做错了什么......没有顶级导入,这就是我的代码的样子: 所以我的项目的根是这样的: export default function A
我是一名优秀的程序员,十分优秀!