我有Redux的React Native App,我正在尝试从组件的构造函数中的Redux获取数据。 我收到这样的错误: 可能未处理的Promise拒绝(id:0):undefined不是对象(评估'library.activeSections')
这是我的代码:
class Accordion extends Component {
...
constructor (props) {
super(props)
this.state = {
activeSections: [this.props.initiallyActiveSection]
}
// if activeSection not specified, default to initiallyActiveSection
let { library } = this.props
if (library.activeSections) { //<-- here is an error library is undefined
this.setState({activeSections: library.activeSections})
}
_toggleSection (section) {
const activeSection = section
let isOpened = this.state.activeSections.indexOf(section) >= 0 ? true : false
var indexInActiveSections = this.state.activeSections.indexOf(section)
if (isOpened === true) {
this.state.activeSections.splice(indexInActiveSections, 1)
} else {
this.state.activeSections.push(section)
}
this.props.libraryActions.setActiveSections(this.state.activeSections)
this.setState({ activeSection })
}
render () {
...
}
}
Accordion.PropTypes = {
library: React.PropTypes.arrayOf(React.PropTypes.number)
}
export default connect(state => ({
library: state.library
}),
(dispatch) => ({
libraryActions: bindActionCreators(libraryActions, dispatch)
})
)(Accordion)
module.exports = Accordion
这里是库reducer redurs / library.js:
import { Map } from 'immutable'
const initialState = Map({
activeSections: [0]
})
export default function library (state = initialState, action = {}) {
switch (action.type) {
case 'SET_ACTIVE_SECTIONS':
return {
...state,
activeSections: action.activeSections
}
default:
return state
}
}
这是库动作actions / library.js:
export const setActiveSections = (activeSections) => {
return {
type: 'SET_ACTIVE_SECTIONS',
activeSections
}
}
这里是reducers / index.js:
import articles from './articles'
import journal from './journal'
import library from './library'
export {
articles,
journal,
library
}
我无法理解为什么this.props中没有库对象
希望得到你的帮助。
答案 0 :(得分:1)
删除第module.exports = Accordion
行