简体   繁体   English

使用ESLint规则反应Redux

[英]React Redux with ESLint rules

in using React and Redux with Airbnb style and ESlint rules I have some confusions mostly about destructuring and upper scope: 在使用具有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);

in this code at destructuring the state, ESLint says Must use destructuring state assignment (react/destructuring-assignment) and when I re-write it as 在此代码中,关于状态的销毁,ESLint说Must use destructuring state assignment (react/destructuring-assignment) ,当我将其重写为

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

again I receive 我再次收到

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

the same type of warnings is shown in the when I deconstruct users(of the store) like const {users}=this.props; 当我解构用户(商店)的用户时会显示相同类型的警告,例如const {users}=this.props; it says 'users' is missing in props validation (react/prop-types) and when I define it in propTypes, it says object is forbidden 它说'users' is missing in props validation (react/prop-types)而当我'users' is missing in props validation (react/prop-types)定义它时,则表示object被禁止

You can "rename" the variable while destructuring to prevent a conflict: 您可以在销毁结构时“重命名”变量以防止冲突:

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

The takes the value from this.state.user and assigns it to currentUser . 接受来自this.state.user的值,并将其分配给currentUser

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑Create-React-App ESLint规则 - Editing Create-React-App ESLint rules 如何在 Create React App 中向 ESLint 添加规则 - how to add rules to ESLint in Create React App 有没有办法更改弹出的 create-react-app 的 eslint 规则? - Is there a way to change eslint rules for ejected create-react-app? 我可以将我的 React.Component 子类添加到 ESLint 规则吗? - Can I add my React.Component subclass to ESLint rules? 是否有任何 eslint 规则可以防止“对象作为 React 子对象无效” - Are there any eslint rules that could prevent `Objects are not valid as a React child` 启用 react/jsx-sort-props 时出错 [eslint-plugin-react 的规则] - Error when enabling react/jsx-sort-props [Rules of eslint-plugin-react] [eslint]'react-redux'应该在项目的依赖项中列出,无法解析模块'react-redux'的路径 - [eslint]'react-redux' should be listed in the project's dependency+unable to resolve path to module 'react-redux' React JS - ESLint - 使用 UNSAFE_componentWillMount、UNSAFE_componentWillUpdate 和 UNSAFE_componentWillReceiveProps 忽略组件的规则 - React JS - ESLint - Rules for ignoring components with UNSAFE_componentWillMount, UNSAFE_componentWillUpdate, and UNSAFE_componentWillReceiveProps 允许`let state = {` - eslint rules that allows `let state = {` 解决似乎没有意义的ESLint / React / Redux(Airbnb配置)错误 - Resolving ESLint / React / Redux (Airbnb config) errors that don't seem to make sense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM