道具类型可以在正常的React组件上正常运行,但在Redux连接的组件上无法运行。
错误详细信息
未被捕获的TypeError:无法设置属性propTypes函数的功能设置(props)
完整的错误详细信息
未捕获的TypeError:无法设置属性propTypes of Function Settings(props){ _classCallCheck(this,Settings);
var _this = _possibleConstructorReturn(thi...<omitted>... } which has only a getter
at Object.eval (Settings.js?384b:533)
at eval (Settings.js:509)
at Object../src/Navigation/Components/Settings.js (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:14403)
at __webpack_require__ (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:725)
at fn (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:102)
at eval (routes.js?5665:15)
at Object../src/routes.js (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:14763)
at __webpack_require__ (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:725)
at fn (main.b2f70c3c5e2d43b8884a.js?b2f70c3c5e2d43b8884a:102)
at eval (index.js?b635:2)
我正在使用prop类型v15,react v16,react-redux v3 lib。
Settings.js组件:
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import React, { Component } from 'react';
class Settings extends Component {
constructor(props) {
super(props);
}
static get propTypes() {
return {
loginId:React.PropTypes.string,
loading: React.PropTypes.bool,
updateEmail: React.PropTypes.func
}
}
render(){
return(
<div>{this.props.text}</div>
)
}
}
function mapStateToProps(state) {
let text = "Hello Settings";
return {
text
}
}
Settings.propTypes = {
loginId: PropTypes.string,
loading: PropTypes.bool
}
export default connect(mapStateToProps, null)(Settings);
答案 0 :(得分:1)
mapStateToProps用于将变量从redux存储发送到react组件。
但是您只是用它来设置道具。
但是,根据Bartek的评论,您可以尝试以下操作:
const mapStateToProps = state => {
return {
text: "hello settings"
}
}
您应该使用mapStateToProps的方式更像这样:
const mapStateToProps = state => {
return {
text: state.text
}
}