我正在构建一个可重用的 React 组件,而不使用 React-App,而且我对 Jest 很陌生。我不断收到这个消息。我在 Stackoverflow 上尝试了几种帖子解决方案,但目前陷入困境:
\n\xe2\x97\x8f 测试套件运行失败
\nJest encountered an unexpected token\n\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere's what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 To have some of your "node_modules" …Run Code Online (Sandbox Code Playgroud) 我有一个带有输入元素的 React DatePicker 组件。为了处理 onChange 事件,我定义了 onChangeHandler 函数。但是,当我想在 onChangeHandler 中定义作为参数传递的所有函数的类型时,我收到以下错误:
“(value: string) => void”类型的参数不可分配给“Dispatch”类型的参数。参数“value”和“value”的类型不兼容。“SetStateAction”类型不能分配给“string”类型。类型 '(prevState: string) => string' 不可分配给类型 'string'.ts(2345
当我将函数定义为“any”时,此错误消失,但当然我们不希望代码中包含任何内容:)。
下面是我的设置。我的主应用程序中有一个名为 Datepicker 的输入组件,它从主应用程序获取状态。
const [value, setValue] = useState('');
它作为道具传递给 DatePicker 组件,如下所示:
return (
<form>
<label htmlFor="date-picker-input">Date:</label><br />
**<DatePicker value={value} setValue={setValue} dateFormat="YYYY/MM/DD" />**
<button type="submit" value="Submit">Submit</button>
</form>
);
Run Code Online (Sandbox Code Playgroud)
DatePicker 组件看起来像这样:
import onChangeHandler from "../utility/onChangeHandler";
interface IDatePickerProps {
applicationMode?: boolean;
value: string;
setValue: (value: string) => void;
dateFormat: DateFormat;}
const DatePicker: React.FC<IDatePickerProps> = (props) => {
const { value, setValue, dateFormat …Run Code Online (Sandbox Code Playgroud) 我在 React 中有一个 setState 函数,我想将它作为参数传递给另一个函数。我想知道如何在 TypeScript 中定义它而不使用“any”作为类型:
到目前为止它看起来像这样:
export const keyDownHandler = (event: React.KeyboardEvent, buttonDate: number, setShowCalendar: any): void => {
// stuff happening
setShowCalendar(false)
}
Run Code Online (Sandbox Code Playgroud)