簡體   English   中英

使用ESLint規則反應Redux

[英]React Redux with ESLint rules

在使用具有Airbnb風格和ESlint規則的React和Redux時,我對解構和上限有一些困惑:

import user from "./userModel";

class MyName extends Component {
  state = { user, size:0 };
render() {
    const {size}=this.state ; 
    const { username, items, location } = this.state.user;
    return (...)}}
MyName.propTypes = {createNewUser: PropTypes.func.isRequired
  // users: PropTypes.object};
const mapStateToProps = state => ({users: state.users});
export default connect(mapStateToProps,{ createNewUser})(MyName);

在此代碼中,關於狀態的銷毀,ESLint說Must use destructuring state assignment (react/destructuring-assignment) ,當我將其重寫為

const {size, user}=this.state ; 
const { username, items, location } = user;

我再次收到

'user' is already declared in the upper scope. (no-shadow)

當我解構用戶(商店)的用戶時會顯示相同類型的警告,例如const {users}=this.props; 它說'users' is missing in props validation (react/prop-types)而當我'users' is missing in props validation (react/prop-types)定義它時,則表示object被禁止

您可以在銷毀結構時“重命名”變量以防止沖突:

const { size, user: currentUser } = this.state;

接受來自this.state.user的值,並將其分配給currentUser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM