对于ESLint-组件应被编写为纯函数感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解reactpreferred/statelessfunction,并且为您提供关于'translatei
对于ESLint-组件应被编写为纯函数感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解react preferred / stateless function,并且为您提供关于'translate is not a function'-react admin 与 react 测试库和 jest 中的错误、BeanCurrentlyInCreationException sqlSessionFactory Requested bean is currently in creation: Is th...、C++ error ''Undefined reference to Class::Function()'' [duplicate]、Error creating bean with name ''sessionFactory'' defined in class path resource [applicationContext.xm的宝贵知识。
本文目录一览:- ESLint-组件应被编写为纯函数(react preferred / stateless function)
- 'translate is not a function'-react admin 与 react 测试库和 jest 中的错误
- BeanCurrentlyInCreationException sqlSessionFactory Requested bean is currently in creation: Is th...
- C++ error ''Undefined reference to Class::Function()'' [duplicate]
- Error creating bean with name ''sessionFactory'' defined in class path resource [applicationContext.xm
ESLint-组件应被编写为纯函数(react preferred / stateless function)
ESLint在react项目上给了我这个错误。
ESLint-组件应被编写为纯函数(react preferred / stateless function)
它指向组件的第一行。
export class myComponent extends React.Component {render() { return ( //stuff here ); }}
如何摆脱这个错误?
答案1
小编典典两种选择。
暂时停用警告
(未经测试;并且有多种方法可以执行此操作。)
// eslint-disable-next-line react/prefer-stateless-functionexport class myComponent extends React.Component { ...}
使用纯无状态组件
返回值就是将要呈现的值(例如,您基本上是在编写基于类的组件的render
方法:
export const myComponent = () => { return ( // JSX here )}
(如果那是您的事,请使用非ES6表示法。)
对于没有其他支持逻辑的此类组件,我更喜欢隐式返回,例如,
export MyComponent = () => <div> // Stuff here </div>
这是优先事项。我要说的是,您应该遵循React命名约定,并保持所有组件以大写字母开头。
ESLint可能会抱怨缺少多行JSX表达式周围的括号,因此请禁用该规则或使用括号。
如果需要道具,它们将作为参数传递给函数:
const MyComponent = (props) => <div> <Something someProp={props.foo} /> </div>export MyComponent
为了方便起见,您可以像往常一样对参数进行解构:
const MyComponent = ({ foo }) => <div> <Something someProp={foo} /> </div>
如果您使用的是本地var,这可以使隐式返回变得容易一些。PropTypes
除非声明它们,否则您会收到有关丢失的ESLint警告。因为它不是一个类,所以您不能简单地staticpropTypes
在该类中使用它,因此必须将它们附加到该函数上(无论如何,这还是很多人喜欢的)。
'translate is not a function'-react admin 与 react 测试库和 jest 中的错误
如何解决''translate is not a function''-react admin 与 react 测试库和 jest 中的错误?
我正在尝试使用 React 测试库和 Jest 编写测试。但是,我收到了 TypeError: translate is not a function
错误。正如 React Admin Documentation 所建议的,我正在使用 useTranslate
钩子。在测试中,我使用 jest.mock
函数来模拟 ''react-admin'' 包,目前所有的测试都是检查组件是否呈现。所有依赖项均已更新。
现在,测试阻塞在带有 useTranslate 钩子的 translate const 上。我尝试以与 translate
相同的方式将 jest.mock
添加到 useTranslate
,但没有骰子。我还做了一个 console.log(typeof translate)
来证明它实际上是一个返回 function
的函数。关于我可以/应该做什么以通过测试的任何建议?
这是我的代码:
// in src/admin/Organization/OrganizationShow.tsx
import * as React from ''react'';
import {
useTranslate,Show,Tab,TabbedShowLayout,TextField,DateField,} from ''react-admin'';
import { HeaderTitle } from ''../../components'';
export const OrganizationShow = (props: any) => {
const translate = useTranslate();
return (
<Show {...props} title={<HeaderTitle i18n="show.title" />}>
<TabbedShowLayout>
<Tab label={translate(''show.details'')}>
<TextField source="name" label={translate(''data.name'')} />
<DateField source="started" label={translate(''data.started'')} />
<DateField
source="ended"
label={translate(''data.ended'')}
emptyText="-"
/>
</Tab>
<Tab label={translate(''show.apps'')} path="apps">
<p>{translate(''page.not_yet'')}</p>
</Tab>
<Tab label={translate(''show.authentication'')} path="auth">
<p>{translate(''page.not_yet'')}</p>
</Tab>
</TabbedShowLayout>
</Show>
);
};
// in src/__tests__/Organization.test.tsx
import React from ''react'';
import { render } from ''@testing-library/react'';
import { OrganizationShow } from ''../admin/Organization/OrganizationShow'';
// Mock React Admin so we don''t have to resolve it
// Do not take this into account for the example
// The tests are valid if react-admin package is installed
jest.mock(''react-admin'',() => ({
useTranslate: (key: string) => key,Show: () => <div />,Tab: () => <div />,TabbedShowLayout: () => <div />,TextField: () => <div />,DateField: () => <div />,}));
// Suppress PropTypes warning by default
const defaultProps = {
location: {},match: {},};
describe(''OrganizationShow'',() => {
it(''App renders successfully'',() => {
render(<OrganizationShow {...defaultProps} />);
});
});
FAIL src/__tests__/Organization.test.tsx
OrganizationShow
✕ App renders successfully (91 ms)
● OrganizationShow › App renders successfully
TypeError: translate is not a function
17 | <Show {...props} title={<HeaderTitle i18n="show.title" />}>
18 | <TabbedShowLayout>
> 19 | <Tab label={translate(''show.details'')}>
| ^
20 | <TextField source="name" label={translate(''data.name'')} />
21 | <DateField source="started" label={translate(''data.started'')} />
22 | <DateField
// in package.json
"devDependencies": {
"@testing-library/jest-dom": "^5.11.10","@testing-library/react": "^11.2.5","@testing-library/user-event": "^13.0.16","@types/jest": "^26.0.22","@types/jwt-decode": "^2.2.1","@types/node": "^14.14.37","@types/react": "^17.0.3","@types/react-dom": "^17.0.3","@types/react-router-dom": "^5.1.7","@typescript-eslint/eslint-plugin": "^4.19.0","@typescript-eslint/parser": "^4.19.0","eslint": "^7.23.0","eslint-config-airbnb": "^18.2.1","eslint-config-prettier": "^7.2.0","eslint-config-react-app": "^6.0.0","eslint-plugin-flowtype": "^5.4.0","eslint-plugin-import": "^2.22.1","eslint-plugin-jsx-a11y": "^6.4.1","eslint-plugin-prettier": "^3.3.1","eslint-plugin-react": "^7.23.1","eslint-plugin-react-hooks": "^4.2.0","jest-junit": "^12.0.0","prettier": "^2.2.1","prettier-eslint": "^12.0.0","react-test-renderer": "^17.0.2","typescript": "^4.2.3"
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
BeanCurrentlyInCreationException sqlSessionFactory Requested bean is currently in creation: Is th...
Springboot 项目集成 h2 databse 遇到的异常
1. 异常现象
Springboot 集成 h2 database,h2 配置如下
spring.datasource.driver-class-name=org.h2.Driverspring.datasource.url=jdbc:h2:~/testspring.datasource.username=saspring.datasource.password=saspring.datasource.schema=classpath:db/*.sqlspring.datasource.initialization-mode=alwaysspring.datasource.continue-on-error=falsespring.h2.console.path=/h2-consolespring.h2.console.enabled=true
当 spring.datasource.schema 配置脚本时,就会抛出异常,具体异常信息为:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ''sqlSessionFactory'': Requ.........
C++ error ''Undefined reference to Class::Function()'' [duplicate]
问题:
This question already has an answer here: 这个问题在这里已有答案:
- What is an undefined reference/unresolved external symbol error and how do I fix it? 什么是未定义的引用 / 未解析的外部符号错误,我该如何解决? 32 answers 32 个答案
I was wondering if anyone could help me out with this - I''m only new to C++ and it''s causing me a fair amount of troubles. 我想知道是否有人可以帮我解决这个问题 - 我只是 C ++ 的新手,这给我带来了相当多的麻烦。
I''m trying to make relatively simple Deck and Card class objects. 我正在尝试制作相对简单的 Deck 和 Card 类对象。
The error is showing up in "Deck.cpp", declaration of an array of cards, and then when i try to fill the array with card objects. 错误显示在 “Deck.cpp”,声明一组卡片,然后当我尝试用卡片对象填充数组时。 It says there''s an undefined reference to Card::Card()
, Card::Card(Card::Rank, Card::Suit)
and Card::~Card()
. 它说有一个未定义的参考 Card::Card()
, Card::Card(Card::Rank, Card::Suit)
和 Card::~Card()
。
I''ve got all my includes seemingly right, so I don''t know what''s going wrong. 我的所有包括看似正确,所以我不知道出了什么问题。
The code is as follows: 代码如下:
deck.h 加入 deck.h
#ifndef DECK_H
#define DECK_H
#include "card.h"
class Deck
{
public:
Deck();
~Deck();
Card DealNextCard();
void Shuffle();
void DisplayDeck();
protected:
private:
};
#endif // DECK_H
deck.cpp deck.cpp
#include "Deck.h"
#include "card.h"
using namespace std;
const int NUM_TOTAL_CARDS = 52;
const int NUM_SUITS = 4;
const int NUM_RANKS = 13;
Card* cardArray;
void Deck() {
cardArray = new Card[NUM_TOTAL_CARDS];
int cardCount = 0;
for (int i = 0; i > NUM_SUITS; i++) {
for (int j = 0; j > NUM_RANKS; j++) {
cardArray[cardCount] = Card(Card::Rank(i), Card::Suit(j) );
cardCount++;
}
}
}
Card DealNextCard();
void Shuffle();
void DisplayDeck();
card.h card.h
class Card
{
public:
enum Suit {D=0, H, C, S};
enum Rank {ONE=0, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, J, Q, K, A};
Card(Card::Rank, Card::Suit);
Card();
virtual ~Card();
Card::Suit suit;
Card::Rank rank;
Card::Rank GetRank();
Card::Suit GetSuit();
std::string CardName();
protected:
private:
};
#endif // CARD_H
card.cpp card.cpp
#include "card.h"
using namespace std;
Card::Suit cardSuit;
Card::Rank cardRank;
void Card() {
//nothing
}
void Card(Card::Rank rank, Card::Suit suit) {
cardRank = rank;
cardSuit = suit;
}
Card::Rank GetRank() {
return cardRank;
}
Card::Suit GetSuit() {
return cardSuit;
}
std::string CardName() {
string test;
test = "testing string";
return test;
}
解决方案:
参考: https://stackoom.com/en/question/13vcbError creating bean with name ''sessionFactory'' defined in class path resource [applicationContext.xm
在编写ssh框架项目的时候出现了“Error creating bean with name ''sessionFactory'' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream”的错误信息。一般都会按照提示来找错误,于是去找Spring的配置文件,但是始终是没有发现错误是什么。然后重新检查了一边实体类的映射文件找到了问题。
实体类:
public class Product {
private Integer pid;
private String pname;
private Double market_price;
private Double shop_price;
private String pdesc;
private Integer is_hot;
private Date pdate;
// set get
}
然后我去我数据库里面找了找。
发现我实体里面少了“image”这个字段,添加上就好了。平时写代码一定要注意下细节,避免犯重复简单的错误。
关于ESLint-组件应被编写为纯函数和react preferred / stateless function的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于'translate is not a function'-react admin 与 react 测试库和 jest 中的错误、BeanCurrentlyInCreationException sqlSessionFactory Requested bean is currently in creation: Is th...、C++ error ''Undefined reference to Class::Function()'' [duplicate]、Error creating bean with name ''sessionFactory'' defined in class path resource [applicationContext.xm的相关信息,请在本站寻找。
本文标签: