[英]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.