我有一个React组件,它使用react-redux connect
函数(装饰器)来设置属性。在JSX中引用此组件时,flowtype抱怨" 属性未在...... React元素的道具中找到"
type SidebarCmpProps = {
activeSidebarComponent: string,
actions: { openCallMember: () => void }
}
@connect(
(state) => {
return { activeSidebarComponent: state.sidebar.activeSidebarComponent }
},
(dispatch) => ({ actions: bindActionCreators({ openCallMember }, dispatch) })
)
class Sidebar extends React.Component {
props: SidebarCmpProps
static propTypes = {
actions: React.PropTypes.object.isRequired,
activeSidebarComponent: React.PropTypes.string
}
}
确切的错误是:
65: ^^^^^^^^^^^ React element `Sidebar` 56: props: SidebarCmpProps ^^^^^^^^^^^^^^^ property `actions`. Property not found in... 65: ^^^^^^^^^^^ props of React element `Sidebar`
要解决此错误,我必须将属性更改为any
的联合类型并添加defaultProps
,这不是理想的
type SidebarCmpProps = {
activeSidebarComponent: any | string,
actions: any | { openCallMember: () => void }
}
class Sidebar extends React.Component {
static defaultProps: SidebarCmpProps = {
actions: null,
activeSidebarComponent: null
}
}
有更好的解决方案吗?
答案 0 :(得分:0)
好吧,您收到property 'actions'. Property not found in
错误,因为您只是在activeSidebarComponent
设置了@connect
道具,并且操作被声明为必需道具actions: React.PropTypes.object.isRequired
。
因此,您必须为actions
道具设置默认值或删除该限制。
答案 1 :(得分:-1)
如果你想让两个组件道具都是可选的,你应该像这样定义它们:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="pdfmake.js"></script>
<script type="text/javascript" src="vfs_fonts.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="raj.js"></script>
<script type="text/javascript" src="jspdf.js"></script>
</head>
<body ng-app="pdfDemo">
<div ng-controller="pdfCtrl">
<div id="pdfContent">
<table id="example-table">
<thead>
<th>firstName</th>
<th>lastName</th>
<th>Gender</th>
<th>Mobile</th>
</thead>
<tbody>
<tr ng-repeat="emp in employees">
<td>{{emp.firstName}}</td>
<td>{{emp.lastName}}</td>
<td>{{emp.gender}}</td>
<td>{{emp.mobile}}</td>
</tr>
</tbody>
</table>
</div>
<button ng-click="openPdf()">Open Pdf</button>
<button ng-click="downloadPdf()">Download Pdf</button>
</div>
</body>
</html>
另外,我不确定提供React的propTypes和flowtype是否是个好主意。那个重复的逻辑,不是吗?