2016-11-15 76 views
0

我有Redux的React Native App,我试图从组件的构造函数中从Redux中获取数据。 我得到的错误是这样的: 可能未处理无极抑制(ID:0):不确定是不是(评估 'library.activeSections')可能未处理的承诺拒绝(id:0):undefined不是对象

这里是我的代码的对象:

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 

这里是图书馆减速机减速机/ 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 
    } 
} 

,这里是图书馆的行动行动/ library.js:

export const setActiveSections = (activeSections) => { 
    return { 
    type: 'SET_ACTIVE_SECTIONS', 
    activeSections 
    } 
} 

这里是减速/ index.js:

import articles from './articles' 
import journal from './journal' 
import library from './library' 

export { 
    articles, 
    journal, 
    library 
} 

我不明白为什么在this.props

希望对你有所帮助没有库对象。

+1

删除行 'module.exports =手风琴',并尝试 – Jickson

+0

它帮助!你是最棒的@Jickson – Dmitry

+0

很高兴知道这对你有所帮助。添加它作为答案,以便稍后帮助他人。 – Jickson

回答

1

删除线module.exports = Accordion

相关问题